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 ...

+2


source to share


3 answers


will this work for you:



return from v in qry
       where buildingStatus.Contains(v.Status)
       select v;

      

+5


source


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;
}

      

+1


source


The approaches suggested are correct in my opinion ... you should also keep the method in mind IEnumerable<T>.Where

.

http://msdn.microsoft.com/en-us/library/bb910023.aspx

+1


source







All Articles