让我解释一下使用此代码示例:
var commands1 = new List<int> { 1 };
var lessons = new List<lesson>
{
new lesson
{
hours = new List<hour>
{
new hour { period = 1 }
}
}
};
List<command> commands2
{
get
{
return (
from o in commands1
select new command
{
hour = ????;
}
).ToList();
}
}
代替????
。我需要获取hour
与period
对应的o
对象。通常我会遍历lessons
,然后遍历hours
来检查hour.period
,但我不知道如何在LINQ查询中执行此操作。
我希望这很清楚(我正确地解释了代码)。
答案 0 :(得分:3)
hour = lessons.SelectMany(l => l.hours).Where(h => h.period == o);