2011-10-06 70 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() 

即直接使得数据表作为枚举。