14

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

[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);
        }
    }             
}
4

2 回答 2

20

从列表中的tblSearches位置获取数据:seaUserIdIDs

[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);
    }             
}
于 2012-12-02T13:55:47.473 回答
6

您可以使用包含

var iq = db.tblSearches.Where(x => Ids.Contains(x.seaUserId));
于 2012-12-02T13:55:58.543 回答