rockyashkumar rockyashkumar - 1 year ago 68
C# Question

two conditions checking in where clause using linq 2 entites

hi i have table called products with columns

prodcut_price( values like 1200,2000,3000,100)

anorther table categories with columns


and entity name is dbcontext...

How can i get the products with category name "a" and product price is >500 and <10000

i am using linq to enitities...

would any pls help on ....

Many thanks....

Answer Source

if you have no foreign key in db you should use Cubicle.Jockey's answer

if you have foreign key in db it would be something like

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

with just lambdas it would be simply

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

if you need a list or array and want close dbcontext you are calling either ToList or ToArray on query