Dynamic Linq Group By

c# dynamic dynamic-linq group-by linq

Question

I am currently building reports and have a need to Group columns dynamically, depending on user's choice. Now, assuming that the situation is fixed on all columns, the query would be as follows:

var groupedInvoiceItems = invoiceItems.GroupBy(x => new { x.SalesInvoice.name, x.SalesInvoice.currencyISO, x.CatalogProduct });

Doing so would return results as desired, IGrouping. I would then run a loop to process the necessary data as below:

foreach (var groupedInvoiceItem in groupedInvoiceItems)
{
    // Perform work here
}

Now, the headache comes in when I try to make the Grouping dynamic by using Dynamic Linq. The query is as follows:

var groupedInvoiceItems = invoiceItems.GroupBy("new (SalesInvoice.name, SalesInvoice.currencyISO, CatalogProduct)", "it");

The problem with this is that it does not return IGrouping anymore. Hence, my foreach loop no longer works. Is there any solution to the matter? I tried casting IGrouping to the Dynamic query but to no avail. Help is needed urgently.

1
1
10/15/2014 2:02:11 AM

Accepted Answer

The result of the GroupBy is an IEnumerable<IGrouping<DynamicClass,InvoiceItem>>, so you can proceed by something like:

foreach (IGrouping<DynamicClass,InvoiceItem> invoiceItemGroup in groupedInvoiceItems)
{

}
4
10/14/2014 2:51:22 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow