LINQ: Checking if field (db) contains elements in ILIST?
I'm trying to create an extension method that I can forward the IList of statuses and check that they exist, I thought the best way to do this is ILIST - but maybe I am wrong? Is this the best way to pass multidimensional items to the A List method? Its a generic LIST, hence no conversion, etc. From Object.
Basically I have it as a signature.
public static IQueryable<Building> WithStatus(this IQueryable<Building> qry,
IList<BuildingStatuses> buildingStatus)
{
//PSUEDO CODE
//return from v in qry
// where v.Status is IN MY LIST called buildingStatus
// select v;
}
and to call it I am using (example below in my TDD), it works great and the values ββgo to my method above.
target.GetBuildings().WithStatus(new List<BuildingFilters.BuildingStatuses>()
{ BuildingFilters.BuildingStatuses.Available,
BuildingFilters.BuildingStatuses.Decommissioned });
so basically i have my list (IList), it comes in extension method with 2 values ββwhich are big, but need to say in LINQ, i have to say
return from v in qry
where v.Status is IN MY LIST called buildingStatus
select v;
Really appreciate any help
in regards to my extension method, it works the way I did similar 1, but only passing type BuildingStatus, hence only 1 ...
source to share
You can use IEnumerable<T>.Contains
an extension method and let your method be more generic:
public static IQueryable<Building> WithStatus(this IQueryable<Building> qry,
IEnumerable<BuildingStatuses> buildingStatus)
{
return from v in qry
where buildingStatus.Contains(v.Status)
select v;
}
source to share