开发者

Linq-to-SQL ToDictionary

开发者 https://www.devze.com 2023-04-10 21:24 出处:网络
I have the following LINQ query: var allocations = from ta in dc.TransactionAllocations where ta.Allocated == false

I have the following LINQ query:

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 开发者_C百科=> new AllocationDictionaryKey()
            {
                ID = a.ID, 
                InvoiceReference = a.InvoiceReference 
            }, 
            a => a.Amount)
    }

But when I try to execute this, the ToDictionary call fails as it's not a supported LINQ-to-SQL operator. The only way around this I have seen is to call ToDictionary at the end of the query, but I only want one property of my anonymous type to be a dictionary!

Any ideas on how to go about doing this?


Have a look at using AsEnumerable. This is designed to get round operators that are not supported by a specific platform. It means that the data will be processed where the code is rather than where the data is though.

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


Quite old, but here goes. I solved my problem with

 from ta in dc.TransactionAllocations.AsEnumerable()

i.e. directly making the datatable as Enumerable.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号