简体   繁体   中英

a simple SQL join in LINQ not working

Consider these 2 classes.

class OrderDetail {int Specifier;}

class Order
{
  OrderDetail[] Details;
}

Now I have a List I want to enumerate over, selecting only the objects with a Specifier of 1. As this is easy in SQL I thought LINQ with a join would be good for this but I dont know how to build the query.

from o in orders join o in o.Details on o.Id equals od.Id where od.Specifier = 1 select od

This gives an error that 'o' does not exist in the current context after join. What am I doing wrong here?

orders.SelectMany(o => o.Details.Where(od => od.Specifier == 1))

In your query you are using same sequence variable name o for details, you should use join od in o.Details . But join is not needed here, because order already contains details:

from o in orderes
from od in o.Details
where od.Specifier == 1
select od

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