zaria khan zaria khan - 24 days ago 7
C# Question

Select and WHERE using LINQ

I have following classes:

public class CatalogResponseRootObject
{
public Plans Plans { get; set; }
}

public class Plans
{
public List<Plan> plan { get; set; }
}

public class Plan
{
public List<PlanPackage> Packages { get; set; }
}
public class PlanPackage
{
public List<Package2> Package { get; set; }
}
public class Package2
{
public string Name { get; set; }
}


I have a working query. But Now I am trying use Where clause to get list of those
cPlans
WHERE
Package2.Name == "Annual"
. Is it possible to retrieve such information using the
Where
clause and how?

CatalogResponseRootObject catalogResponse = response;
qualifiedPlans = (from cPlans in catalogResponse.Plans.plan
join qPlans in conversationIDResponsePlans.plan on cPlans.Id equals qPlans.Id
select cPlans).ToList();

Answer

So plans have packages that have packages and there should be at least one that has the Name "Annual"? You can add a condition:

where cPlans.Packages.Any(p1 => p1.Package.Any(p2 => p2.Name == "Annual"))

This will return the plans that have at least one package with at least one package that has the name "Annual".