简体   繁体   中英

Linq to SQL Join and Where

I have Users->Orders tables (one to many) and would like to select all users which have specified Orders. I have tried Linq below but it fails with error. How to write such Linq query?

DataAccess.Instance.Users.Where(p => p.Orders.Where(o => o.ProductId == productId))

You should try:

DataAccess.Instance.Users.Where(p => p.Orders.Any(o => o.ProductId == productId))

Note that the second Where is changed to Any which returns a boolean value and satisfies the type of the expression tree expected by the first Where : Where needs a condition, not a set of values retrieved from elsewhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM