How can I create a dynamic LINQ query in C# with possible multiple group by clauses?

Posted by FordPrefect141 on Stack Overflow See other posts from Stack Overflow or by FordPrefect141
Published on 2010-05-24T18:13:13Z Indexed on 2010/05/24 18:31 UTC
Read the original article Hit count: 386

Filed under:
|
|
|
|

I have been a programmer for some years now but I am a
newcomer to LINQ and C# so forgive me if my question sounds
particularly stupid.

I hope someone may be able to point me in the right
direction. My task is to come up with the ability to form a
dynamic multiple group by linq query within a c# script using
a generic list as a source.

For example, say I have a list containing multiple items with the following
structure:

FieldChar1 - character
FieldChar2 - character
FieldChar3 - character
FieldNum1 - numeric
FieldNum2 - numeric

In a nutshell I want to be able to create a LINQ query that
will sum FieldNum1 and FieldNum2 grouped by any one, two or
all three of the FieldChar fields that will be decided at
runtime depending on the users requirements as well as selecting the FieldChar fields in the same query.

I have the dynamic.cs in my project which icludes a GroupByMany extension method but I have to admit I am really not sure how to put these to use. I am able to get the desired results if I use a query with hard-wired group by requests but not dynamically.

Apologies for any erroneous nomenclature, I am new to this language but any advice would be most welcome.

Many thanks

Alex

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET