开发者

LINQ: Checking if a field (db) contains items in an ILIST?

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


would this work for you:

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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜