用Linq(C#,Linq)过滤嵌套数据。

huangapple go评论71阅读模式
英文:

Filter nest data by Linq (C#, Linq)

问题

在方法中,我获取到了一组汽车对象:

public class Cars
{
    public int CarId { get; set; }
    public ClientList[] AvaList { get; set; }
}

public class ClientList
{
    public Client[] ClientData { get; set; }
}

public class Client
{
    public int ClientId { get; set; }
}

在这段代码中,carsForClientIdIEnumerable<Client> 类型。而我想要得到的是只包含 ClientId = 10 数据的 IEnumerable<Cars>

如果您需要帮助修改代码以获得所需的结果,请提出具体的问题。

英文:

I have objects:

public class Cars
{
 public id CarId {get;set;}
 public ClientList[] AvaList {get;set;}
}

public class ClientList
{
 Client[] ClientData {get;set;}
}

public class Client
{
 int ClientId {get;set;}
}

In method Im geting list of Cars:

string cId = 10;
IEnumerable&lt;Cars&gt; cars = GetCars();
var carsForClientId = cars.AvaList.SelectMany(c =&gt; c.ClientData.Where(x =&gt; x.ClientId == cId));

But carsForClientId is IEnumerable&lt;Client&gt;. And I want to get IEnumerable&lt;Cars&gt; contains only data for CliendId = 10;

答案1

得分: 1

你必须选择汽车:

var carsForClientId = cars
    .Where(c => c.AvaList
        .Any(clist => clist.ClientData
            .Any(cl => cl.ClientId == cId)));
英文:

You have to select cars:

var carsForClientId = cars
    .Where(c =&gt; c.AvaList
        .Any(clist =&gt; clist.ClientData
            .Any(cl =&gt; cl.ClientId == cId)));

huangapple
  • 本文由 发表于 2023年3月9日 22:44:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686144.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定