我對此有點新。好奇在以下情況會發生什麼?LINQ to SQL推遲執行和實現
var q = //MY LINQ TO SQL QUERY.Select(...)
.........
.........
var c = q.Count();
.........
.........
var x = q.Where(....).Select(....);
var y = x.ToList();//or something such that forces materialization
var d = q.Count();//same as c
var e = x.Count();
var f = y.Count();
sql語句實際上有多少次訪問數據庫?一旦在Count()。再次在Where()?或者Linq保留Count()期間實現的內容?
或者它也取決於哪裏(..)有什麼?就像它再次引用數據庫vs,它只是引用了作爲'q'/或任何其他.net集合等的一部分獲得的內容?
編輯:
更新我的代碼以一對夫婦的其他情形。當你調用Count
它並沒有實現整個數據集
q -no db trip
c -yes, but translates to aggregate qry - select Count(*) and not a result set (as per answer below)
x -no db trip. No matter what is written in the Where(..)
y - yes
d - yes - does not *reuse* c
e - yes - select count(*) EVEN THOUGH x already materized during y
f - no db trip