开发者

Find specific object in list in object in a LINQ query

开发者 https://www.devze.com 2023-01-16 23:24 出处:网络
Let me explain using this code sample: var commands1 = new List<int> { 1 }; var lessons = new List<lesson>

Let me explain using this code sample:

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

and in place of the ????. I need to get the hour object of which period corresponds to o. Normally I would loop through lessons, then loop through hours to check hour.period but I don't know how to do that in a LINQ query.

I hope that is clear enough (and that I paraphrased the code correctly).


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

精彩评论

暂无评论...
验证码 换一张
取 消