假设我们有一个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);
}
}
}
答案 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));