Reputation: 27
I have the below class data in c#. below is the json format for the same.
{
"Data": {
"plans": [
{
"pDecision": "E",
"pNumber": 123,
"car": [
{
"iNumber": 1,
"liList": [
{
"CID": 112658799,
"liDecision": "A"
},
{
"CID": 112658800,
"liDecision": "A"
}
]
},
{
"iNumber": 2,
"liList": [
{
"CID": 1,
"liDecision": "E"
},
{
"CID": 2,
"liDecision": "A"
}
]
}
]
}
]
}
}
I want to get the details of car and plan. Car object should filter with
"CID": 1 and "liDecision": "E". tried using any but its returing all fields. Since this is collection
Data.plan.where(p=>p.car.any(c=>c.lilist.any(l=>l.cid==1 && l.lidecesion=="E"))); tried this one.
any help?
Upvotes: 0
Views: 576
Reputation: 108
Your Where
returns an enumerable of plans, not cars, and your plan instance contains two cars. If you want to filter cars you need to apply your Where
to cars collection
Data.plans.SelectMany(p => p.car)
.Where(c => c.lilist.Any( li => li.cid==1 && li.lidecesion=="E"));
if you also need a plan that contains that car
Data.plans.SelectMany(p => p.car.Select(c => new {Car = c, Plan = p})) // this returns cars with according plans
.Where(x => x.Car.lilist.Any(li => li.cid==1 && li.lidecesion=="E"));
You will get objects-pairs { Car, Plan }, Plan property will still have the full collection of cars, but Car should satisfy your filter.
Upvotes: 2
Reputation: 1820
Thanks for mentioning what you have already tried :-). May be you can add it to the original question then keeping as a comment ???
You have nested lists and one approach would be to flatten them out and then filter on the flat results
Consider the following code, it first flats Plans then LiLists and finally does the filtering on result
var plans = carPlans.Data.Plans.SelectMany(plan => plan.Car)
.SelectMany(e => e.LiList).Where(list => list.Cid == 1 && list.LiDecision == "E");
Hope this helps
Good luck
Upvotes: 0