With these simple classes:
public class Object
{
List<Sheet> Sheets { get; set; }
}
public class Sheet
{
List<Line> Lines { get; set; }
}
public class Line
{
int LineId { get; set; }
}
I am trying to build a LINQ query that finds the single sheet with a corresponding lineId. In this case, a lineId is unique to one sheet. That is, a sheet can have multiple lines, but no two sheets can share identical lines.
The basic foreach
code looks something like this, given an Object:
Sheet targetSheet = null;
foreach (var sh in Sheet)
{
foreach (var l in sh.Lines)
{
if (l.Id == command.LineId)
{
targetSheet = sh;
break;
}
}
}
My (broken) attempt at a LINQ query with Lambda expressions:
Sheet targetSheet = Sheets.SingleOrDefault(s => s.Lines.Where(line => line.Id == command.LineId));
Can anyone see help me see what I am missing with this lambda expression?