本來我是合併兩組結果有這樣的代碼:當我做ToList()時,爲什麼這個LINQ to SQL查詢會中斷?
var list1 = from a in IDataSourceObject
blahblah
select a;
var list2 = from a in IDataSourceObject2
blahblah
select a;
var joinedlist = from a in list1
join b in list2 on a.id = b.id
into fishcakes
from b in fishcakes.DefaultIfEmpty()
orderby b.ranking
select new { blah=cakes, etc. }
這用來做工精細,但後來我想過濾列表1多一點,所以我這樣做:
var list1 = from a in IDataSourceObject
blahblah
select a;
// ToList required because im calling a method in my code
var updatedList1 = from a in list1.ToList()
where myMethod(somestuff) == true
select a;
var list2 = from a in IDataSourceObject2
blahblah
select a;
var joinedlist = from a in updatedList1
join b in list2 on a.id = b.id
into fishcakes
from b in fishcakes.DefaultIfEmpty()
orderby b.ranking
select new { blah=cakes, etc. }
但是我收到一個錯誤,基本上說OrderBy b.ranking爲null。在完成ToList之後,它不再合併結果。我已經檢查updatedList1,並且我讓myMethod總是返回true,所以本質上來自使用ToList()的問題。
我明白這可能與延遲執行有關,但我不知道怎麼做。它應該完全一樣。
有沒有人有任何建議?
試圖找到考慮到這一點,你與遠方的一個連接的記憶中珍藏的解決方案! – 2010-12-03 16:05:02