简体   繁体   English

第 3 个子级的 Linq 查询

[英]Linq query on 3rd child level

I have following requirement on List of objects.我对对象列表有以下要求。 My class structure is like:我的班级结构是这样的:

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

I want to filter based on Hobbies on following scenarios:我想在以下场景中根据爱好进行过滤:

  • List of All the Standards with Students who has hobby of Playing Music对有演奏音乐爱好的学生的所有标准清单
  • List of All the Standards with Students who has hobby of Singing Songs对有唱歌爱好的学生的所有标准清单

Currently I have List available with me with all child classes and details.目前,我有可用的列表,其中包含所有子类和详细信息。 I am able to do this with ForEach loop but wanted to learn Linq syntax for this scenarios.我可以用 ForEach 循环来做到这一点,但想学习这个场景的 Linq 语法。

Please suggest Linq syntax for this.请为此建议 Linq 语法。

Given you have a collection of Standard called standards ,鉴于你有一个集合的Standard称为standards

List of All the Standards with Students who has hobby of Playing Music对有演奏音乐爱好的学生的所有标准清单

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

List of All the Standards with Students who has hobby of Singing Songs对有唱歌爱好的学生的所有标准清单

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