Serve Laurijssen
Serve Laurijssen

Reputation: 9733

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?

Upvotes: 0

Views: 48

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236228

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

Upvotes: 1

Related Questions