簡體   English   中英

第 3 個子級的 Linq 查詢

[英]Linq query on 3rd child level

我對對象列表有以下要求。 我的班級結構是這樣的:

  public class Standard
    {
    public long Id {get;set;}
    public string Name {get;set;}
    public List<Student> Students {get;set;}
    }
    
    public class Student
    {
    public long Id {get;set;}
    public string Name {get;set;}
    public List<Hobby> Hobbies {get;set;}
    }
    
    public class Hobby
    {
    public long Id {get;set;}
    public string Name {get;set;}  
    }

我想在以下場景中根據愛好進行過濾:

  • 對有演奏音樂愛好的學生的所有標准清單
  • 對有唱歌愛好的學生的所有標准清單

目前,我有可用的列表,其中包含所有子類和詳細信息。 我可以用 ForEach 循環來做到這一點,但想學習這個場景的 Linq 語法。

請為此建議 Linq 語法。

鑒於你有一個集合的Standard稱為standards

對有演奏音樂愛好的學生的所有標准清單

var result = standards.Where(s => s.Students.Any(u => u.Hobbies.Any(h => h.Name == "Playing Music")));

對有唱歌愛好的學生的所有標准清單

var result = standards.Where(s => s.Students.Any(u => u.Hobbies.Any(h => h.Name == "Playing Music")));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM