英文:
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; }
}
在这段代码中,carsForClientId
是 IEnumerable<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<Cars> cars = GetCars();
var carsForClientId = cars.AvaList.SelectMany(c => c.ClientData.Where(x => x.ClientId == cId));
But carsForClientId
is IEnumerable<Client>
. And I want to get IEnumerable<Cars>
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 => c.AvaList
.Any(clist => clist.ClientData
.Any(cl => cl.ClientId == cId)));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论