简体   繁体   中英

Linq-to-SQL ToDictionary

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 => 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()

ie directly making the datatable as Enumerable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM