0

我想知道我是否可以在下面使用数组而不是编写多个 OR

from d in db.tblEquipments.Include(t => t.User).Include(t => t.ChangeLog).AsEnumerable()
                                    where (d.AssetType == "Laptop" || d.AssetType == "Workstation" || d.AssetType == "Mobile" d.AssetType == "Monitor" d.AssetType == "Other Peripheral" || d.AssetType == "Home Printer" || d.AssetType == "Home Router" || d.AssetType == "Removable Device")
                                            (d.Active == 1) && 
                                            (d.Deleted != 1 || d.Deleted == null) && 

像下面这样的东西?

string[] arrItems = new string[] { "Laptop", "Workstation", "Mobile" }; etc
where (d.assetType == arrItems) &&
         (d.Active == 1) && ....

那可能吗?

4

2 回答 2

4

使用Contains方法:

from d in db.tblEquipments.Include(t => t.User).Include(t => t.ChangeLog).AsEnumerable()
where arrItems.Contains(d.AssetType) &&  // same as SQL operator IN
      (d.Active == 1) && 
      (d.Deleted != 1 || d.Deleted == null) && 

也不要使用AsEnumerable()它将所有过滤都带入内存(即不是只传输所需的设备,而是通过网络传输所有设备,并在计算机内存中过滤它们)。

于 2013-07-22T11:40:55.680 回答
0

是的。你有两种方法可以做到这一点。琐碎的方法:

from d in myQueryable
where (arrItems.Contains(d.assetType)) &&
      (d.Active == 1) && ....

前一种方法的问题是它检查所有对(x in myEnumerable, y in arrItems),这导致复杂性O(n²)

最好的方法,复杂度为O(n)

from d in myQueryable
join arrItem in arrItems on d.AssetType equals arrItem
select d
于 2013-07-22T11:45:56.760 回答