2011-10-06 78 views
2

我有以下LINQ查詢:LINQ到SQL ToDictionary

var allocations = 
    from ta in dc.TransactionAllocations 
    where ta.Allocated == false 
    group ta by new { ta.ReceiptReference, ta.Customer } into tag 
    select new 
    { 
     Customer = tag.Key.Customer, 
     ReceiptReference = tag.Key.ReceiptReference, 
     Invoices = tag.ToDictionary(a => new AllocationDictionaryKey() 
      { 
       ID = a.ID, 
       InvoiceReference = a.InvoiceReference 
      }, 
      a => a.Amount) 
    } 

但是,當我嘗試執行這一點,ToDictionary調用,因爲它不是一個支持LINQ到SQL操作失敗。解決這個問題的唯一方法是在查詢結束時調用ToDictionary,但我只希望匿名類型的一個屬性成爲字典!

關於如何去做這件事的任何想法?

回答

3

看看使用AsEnumerable。這樣做的目的是爲了獲得不受特定平臺支持的運營商。這意味着數據將在代碼的位置進行處理,而不是在數據的位置。

Invoices = tag.AsEnumerable().ToDictionary(a => new AllocationDictionaryKey() { ID = a.ID, InvoiceReference = a.InvoiceReference }, a => a.Amount) 
+0

謝謝 - 我仍然得到相同的錯誤,雖然 – Chris

+0

嘗試'tag.ToArray()。ToDictionary' – Steven

+0

如果@史蒂文的建議不起作用,那麼它可能是最初的'集團由',翻譯不好在這種情況下,您必須首先將所有數據加載到內存中 –

2

相當古老,但在這裏。 我解決我的問題與

from ta in dc.TransactionAllocations.AsEnumerable() 

即直接使得數據表作爲枚舉。