LINQ Dynamic Group by inquiry

c# dynamic-linq linq visual-studio

Question

As I attempt to develop a LINQ groupby expression, I am utilizing Dynamic LINQ from the VS2010 code examples. I want to provide the user the option to choose the attributes to group on or by and the grouping duration at runtime (year, quarter, month). I made the decision to adopt Dynamic LINQ as a result.

A portion of my data is shown here:

Identity | Income | Date |
1 | 900000 | 1-1-2000 |
2 | 180000 | 1-9-2000 |
3 | 300000 | 3-2-2002 |
4 | 100000 | 2-7-2003 |

and my expression is as follows:

dataSource.GroupBy("new (OrDate.Year as Year, OrDate as Month)", "Income").Select("new (Key.Year, Key.Month, Sum(Value) as Income)");

ONLY IF I KNOW EXACTLY WHAT TYPE OF LIST IT IS, DOES THIS WORK; for instance:List<Product> or List<Order> . The dataSource type at build time is an issue for me.List<object> . As a result, anytime I add Orders or Products to the list, I receive an exception:

No generic method 'GroupBy' on type 'System.Linq.Queryable' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic.

Can somebody explain how to prevent this while maintaining the source list as aList<object> ?

1
3
3/20/2011 6:52:55 PM

Popular Answer

Attempt this:

For group by in LinQ

var x = t.GroupBy(gp => gp.Name).OrderBy(group => group.Key).Select(group => Tuple.Create(group.Key, group.Count()));

Please locate

https://dotnetfiddle.net/WPqzhJ

0
9/1/2015 12:45:08 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