2015-08-20 63 views
0

我有以下IQueryable其中,有多個評估,但我注意到這導致9個查詢得到執行對服務器?實際上它只應該是一個?當應用多個where子句時執行IQueryable多個查詢

var datafound = db.CustomersData 
    .Where(x => x.EntryDate == eod.EntryDate); 

if (!string.IsNullOrWhiteSpace(eod.Type)) 
    datafound = datafound.Where(x => x.Type == eod.Type); 

if (!string.IsNullOrWhiteSpace(eod.LastName)) 
    datafound = datafound.Where(x => x.LastName.Contains(eod.LastName)); 

if (!string.IsNullOrWhiteSpace(eod.Address)) 
    datafound = datafound.Where(x => x.Address.Contains(eod.Address)); 

if (!string.IsNullOrWhiteSpace(eod.BuildingName)) 
    datafound = datafound.Where(x => x.BuildingName.Contains(eod.BuildingName)); 

    ..... 
    ..... 


return datafound.ToList(); 
+3

您是如何得出結論導致9個查詢的? –

+0

@TravisJ我同意 - 即使「CustomersData」不是'IQueryable',我也看不到這會執行9次。我們可以看到它正在運行的查詢嗎? – Rob

+0

不與'IQueriable'。如果你的「...」不隱藏任何額外的邏輯。 'ToList()'會「執行」(如果我能這麼說)你的實際查詢。 –

回答

0

邏輯有.ToList(前fillinViewBag法)和內fillinViewBag之前,而不是有針對的.sum .AVG等

的fillinViewBag法若干評價只需要在.Tolist()之後,這使Query 1和fillinViewBag方法的執行與內存中的數據一起工作。

感謝您的快速反饋。