清洁linq实现,按孙子过滤列表



我有一个用户列表,这些用户附加到包含客户端的应用程序。我希望通过Linq和am旋转过滤应用程序和客户端的用户列表。

理想情况下,我会在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; }
}

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

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

最新更新