在LINQ查询中的对象列表中查找特定对象

时间:2010-09-16 19:52:45

标签: c# .net linq c#-3.0

让我解释一下使用此代码示例:

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();
   }
}

代替????。我需要获取hourperiod对应的o对象。通常我会遍历lessons,然后遍历hours来检查hour.period,但我不知道如何在LINQ查询中执行此操作。

我希望这很清楚(我正确地解释了代码)。

1 个答案:

答案 0 :(得分:3)

hour = lessons.SelectMany(l => l.hours).Where(h => h.period == o);