rockyashkumar rockyashkumar - 3 months ago 15
C# Question

two conditions checking in where clause using linq 2 entites

hi i have table called products with columns

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


anorther table categories with columns

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


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 this..one ....

Many thanks....

Answer

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 p.category.name == 'a' 
   && p.product_price > 500 
   && p.product_price < 10000
select p;

with just lambdas it would be simply

  var query = dbcontext.Products.Where(p => p.category.name == '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

Comments