繁体   English   中英

C#如何使用LINQ获取具有条件的子集合项的数量

[英]C# How to get number of child collection items with condition using LINQ

给定一个List<CourseSchedule>变量,如何让元素在所有的藏品总数List<CourseSection>与给定SemesterID使用LINQ所有课程? 在这种情况下可能吗?

类结构如下:

class CourseSchedule
{
   public Course Course {get;set;}
   public List<CourseSection> Sections {get;set}
}

class Course
{
   public int SemesterID {get;set};
   public string SemesterName {get;set;}
}

class CourseSection
{
   public int SectionID {get;set;}
   public string SectionType {get;set;}
}

希望我明白你的意图。 以下应该对您有所帮助。

list.Where(x=>x.Course.SemesterID==semesterId).SelectMany(x=>x.Sections).Count();

或者(如 juharr 指出的那样)

list.Where(x=>x.Course.SemesterID==semesterId).Sum(x=>x.Sections.Count);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM