views:

101

answers:

3

Hi there,

I am trying to create an extension method that i can forward a IList of statuses and check weather they exist, I thought the best way to do this was an ILIST - but maybe i wrong? Is this the best way to pass multple items to a method - A List? Its a generic LIST so hence no conversion etc from Object.

Basically I have this 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 use (example below is in my TDD), it works great the values arrive in my method above.

    target.GetBuildings().WithStatus(new List<BuildingFilters.BuildingStatuses>() 
           { BuildingFilters.BuildingStatuses.Available,
             BuildingFilters.BuildingStatuses.Decommissioned });

so basically i have my list (IList) it arrives in the extension method with 2 values which is great but need to say in LINQ i need to say

        return from v in qry
               where v.Status is IN MY LIST called buildingStatus
               select v;

Really appreciate any help,

with regards to my extension method, it works as i have done similar 1 but only passing type BuildingStatus hence only 1...

+5  A: 

would this work for you:

return from v in qry
       where buildingStatus.Contains(v.Status)
       select v;
John Boker
+1  A: 

You can use the IEnumerable<T>.Contains extension method and allow your method to be more versatile:

public static IQueryable<Building> WithStatus(this IQueryable<Building> qry,
          IEnumerable<BuildingStatuses> buildingStatus)
{

    return from v in qry
           where buildingStatus.Contains(v.Status)
           select v;
}
bruno conde
+1  A: 

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

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

sebastian