gpt4 book ai didi

c# - 干净的 linq 实现以按孙子过滤列表

转载 作者:行者123 更新时间:2023-12-02 21:29:30 24 4
gpt4 key购买 nike

我有一个附加到包含客户端的应用程序的用户列表。我希望通过 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; }
}

最佳答案

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

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

关于c# - 干净的 linq 实现以按孙子过滤列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22672574/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com