Reputation: 2591
I was wondering if i could use an array for the below instead of writing multiple ORs
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) &&
Something like the below?
string[] arrItems = new string[] { "Laptop", "Workstation", "Mobile" }; etc
where (d.assetType == arrItems) &&
(d.Active == 1) && ....
is that possible?
Upvotes: 0
Views: 53
Reputation: 13022
Yes it is. You have two ways to do it. The trivial way:
from d in myQueryable
where (arrItems.Contains(d.assetType)) &&
(d.Active == 1) && ....
The problem with the previous method is that it checks all pair (x in myEnumerable, y in arrItems)
which leads to a complexity of O(n²)
And the best way, with a complexity of O(n)
:
from d in myQueryable
join arrItem in arrItems on d.AssetType equals arrItem
select d
Upvotes: 0
Reputation: 236208
Use Contains
method:
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) &&
Also don't use AsEnumerable()
it brings all filtering into memory (i.e. instead of transferring only required equipments you are transferring all equipments over the network, and filter them in computer memory).
Upvotes: 4