C# 如何添加查找值数组的where子句
我想知道我是否可以为下面的代码使用一个数组,而不是编写多个ORC# 如何添加查找值数组的where子句,c#,linq,entity-framework,C#,Linq,Entity Framework,我想知道我是否可以为下面的代码使用一个数组,而不是编写多个OR from d in db.tblEquipments.Include(t => t.User).Include(t => t.ChangeLog).AsEnumerable() where (d.AssetType == "Laptop" || d.AssetType == "Workstation" || d.AssetType == "Mobil
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) && ....
这可能吗?使用
包含方法:
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()
它将所有过滤带到内存中(即,您不是只传输所需的设备,而是通过网络传输所有设备,并在计算机内存中进行过滤)。使用包含方法:
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()
它会将所有过滤带到内存中(即,不是只传输所需的设备,而是通过网络传输所有设备,并在计算机内存中进行过滤)。是的。你有两种方法。琐碎的方式:
from d in myQueryable
where (arrItems.Contains(d.assetType)) &&
(d.Active == 1) && ....
前面的方法的问题是它检查所有对(myEnumerable中的x,arrItems中的y)
,这导致复杂性为O(n²)
最好的方法是,复杂度为
O(n)
:
是的。你有两种方法。琐碎的方式:
from d in myQueryable
where (arrItems.Contains(d.assetType)) &&
(d.Active == 1) && ....
前面的方法的问题是它检查所有对(myEnumerable中的x,arrItems中的y)
,这导致复杂性为O(n²)
最好的方法是,复杂度为
O(n)
:
但是,我会更改顺序,以便您在“包含”比较之前先筛选“活动”和“删除”。但是,我会更改顺序,以便您在“包含”比较之前先筛选“活动”和“删除”。