清理linq实现以过滤孙子列表

Jak*_*ckl 2 c# linq

我有一个附加到包含客户端的应用程序的用户列表.我希望通过Linq过滤应用程序和客户端的用户列表并正在旋转.

理想情况下,我将使用单个语句,其中Application.Name =="example"也在ClientApp.Id == 1中.

这是我到目前为止的地方,但是我有一些关于嵌套的内部大脑问题.任何帮助表示赞赏

var users2 = users.Where(x => x.App.Select(y => y.Name).Contains("example"));

public class User
{
    public string FirstName { get; set; }
    public List<Application> App { get; set; }
}
public class Application
{
    public string Name { get; set; }
    public List<ClientApp> Client { get; set; }
}
public class ClientApp
{
    public string Id { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

Ree*_*sey 5

您可以使用嵌套调用Enumerable.Any来过滤:

var filtered = users.Where(u => 
                   u.App.Any(
                      a => a.Name == "example" 
                        && a.Client.Any(c => c.Id == 1)));
Run Code Online (Sandbox Code Playgroud)