IQueryable <t>按ID列表过滤</t>

时间:2012-12-02 13:54:08

标签: c# iqueryable

假设我们有一个IQueryable<T>Where子句可以按单个ID值进行过滤,但如何根据ID列表返回IQueryable

[TestMethod]
public void TestIQueryableWithList()
{
    int ID1 = 1;
    List<int> IDs = new List<int> { 1, 3, 4, 8 };

    using (var db = new SellooEntities())            
    {
        // works fine as single value
        var iq = db.tblSearches.Where(x => x.seaUserId == ID1);

        // how can i do it to check for all the IDs ??
        foreach(int ID in IDs)
        {
            // this obviously wont work
            var iq = db.tblSearches.Where(x => x.seaUserId == ID);
        }
    }             
}

2 个答案:

答案 0 :(得分:16)

tblSearches获取seaUserId IDs列表中的数据:

[TestMethod]
public void TestIQueryableWithList()
{
    int ID1 = 1;
    List<int> IDs = new List<int> { 1, 3, 4, 8 };

    using (var db = new SellooEntities())            
    {
        var iq = db.tblSearches.Where(x => IDs.Contains(x.seaUserId);
    }             
}

答案 1 :(得分:6)

您可以使用Contains

var iq = db.tblSearches.Where(x => Ids.Contains(x.seaUserId));