Using linq 2 entities, two criteria are checked in the where clause.

.net c# entity-framework linq linq-to-entities

Question

Hello, I have a table with columns named products.

                 product_id
                 prodcut_name
                 prodcut_price( values like 1200,2000,3000,100)
                 prodcut_desc
                 category_id(1,2,3,.....) 

other column-based table types

              category_id(1,2,3.......)
              category_name(a,b,c....)

dbcontext is the name of the entity.

How can I get items from category "a" with prices between $500 and $100,000?

I'm using linq to entities.

could someone please assist with this one?

Thanks a lot.

1
11
9/2/2011 9:50:50 PM

Accepted Answer

The logic operators you will employ in C# are those listed below if you are seeking for them:

  • && logical AND
  • || logical OR
  • ! logical NOT
  • ^ logical (bitwise)XOR
  • COND ? TRUE-STATEMENT : FALSE-STATEMENT operator in ternary

Your composite condition will thus appear as follows in pseudocode:

product_price > 500 AND product_price < 10000

When your context is built, it will only include DBSets and no navigation properties if your database does not contain any foreign keys. Therefore, using Cubicle is your only choice. Jockey's response.

If your database has foreign keys, your entity objects will have navigation attributes and you will be able to accomplish the following:

var query = from p in dbcontext.products
where p.category.name == 'a' 
   && p.product_price > 500 
   && p.product_price < 10000
select p;

You can also utilize the straight LINQ extension methods:

  var query = dbcontext.Products.Where(p => p.category.name == 'a' 
                                         && p.product_price > 500 
                                         && p.product_price < 10000);

If you wish to terminate the database context and require a list or array, you are calling eitherToList or ToArray on query :

var products = query.ToList();

With Entity Framework (EF), there is an alternate method available thanks to the built-in EntitySQL query language. You'll use a similar phrase when you say it:

var query = ProductsQuery.Where("it.Category.Name == 'a' AND it.Product_Price BETWEEN 500 AND 10000");

where ProductsQuery is ObjectQuery<Product> .

29
2/27/2018 11:32:32 PM

Popular Answer

from p in dbcontext.products
join c in dbcontext.category on p.category_id equals c.category_id
where p.product_price > 500 && p.product_price < 10000 && c.Name == "a"
select p

You will get an IQueryable with the filtered results after doing this.

According to Vittore's response below, the following version would be more acceptable if there were foreign key constraints:

from p in dbcontext.products
where p.category.name =='a' && p.product_price > 500 && p.product_price < 10000
select p;


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow