我們的前端UI有一個過濾系統,在後端運行數百萬行。它使用在邏輯過程中構建的IQueryable,然後一次執行所有內容。每個單獨的UI組件都進行了與運算(例如,Dropdown1和Dropdown2將只返回具有共同選定內容的行)。這不是問題。但是,Dropdown3中有兩種類型的數據,檢查的項目需要ORd在一起,然後與查詢的其餘部分進行與運算。Linq查詢超時,如何簡化查詢
由於大量的行正在進行操作,它會保持超時。由於需要發生一些額外的連接,所以這有點棘手。這裏是我的代碼,用表名稱進行替換:
//The end list has driver ids in it--but the data comes from two different places. Build a list of all the driver ids.
driverIds = db.CarDriversManyToManyTable.Where(
cd =>
filter.CarIds.Contains(cd.CarId) && //get driver IDs for each car ID listed in filter object
).Select(cd => cd.DriverId).Distinct().ToList();
driverIds = driverIds.Concat(
db.DriverShopManyToManyTable.Where(ds => filter.ShopIds.Contains(ds.ShopId)) //Get driver IDs for each Shop listed in filter object
.Select(ds => ds.DriverId)
.Distinct()).Distinct().ToList();
//Now we have a list solely of driver IDs
//The query operates over the Driver table. The query is built up like this for each item in the UI. Changing from Linq is not an option.
query = query.Where(d => driverIds.Contains(d.Id));
我如何可以簡化這個查詢,使我沒有找回ID的成千上萬到內存中,然後給他們回SQL?