Linq-to-SQL ToDictionary

时间:2011-10-06 08:14:12

标签: c# linq linq-to-sql todictionary

我有以下LINQ查询:

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-to-SQL运算符。我所看到的唯一方法就是在查询结束时调用ToDictionary,但我只希望我的匿名类型的一个属性成为字典!

关于如何做到这一点的任何想法?

2 个答案:

答案 0 :(得分:3)

看一下使用AsEnumerable。这旨在获得特定平台不支持的循环运算符。这意味着将在代码所在的位置处理数据,而不是处理数据的位置。

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

答案 1 :(得分:2)

相当古老,但这里有。 我用

解决了我的问题
 from ta in dc.TransactionAllocations.AsEnumerable()

即。直接将数据表设为Enumerable。

相关问题