簡體   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