Solved

Dynamic Group By in Linq

Posted on 2009-03-31
1
1,898 Views
Last Modified: 2013-11-11
Background: I have a class 'car', which amongst other things, has 2 properties which are, the number of doors and the number of seats. If I then create and enumerable List<car> of cars I want to group cars based on a selection made by the user. The user's selection will post back a string that I use in switch statement.
Is it possible to dynamically add a group by parameter to a Linq statement based on the user's selection or would I need to have a distinct Linq statement for each possible selection the user makes?
There could be any number of group by's and I don't want to repeat loads of code when the logic is identical each time, only the group by parameter changes.

The code I've stuck in below is an example; in reality the 'MORE STUFF TO DO' part is pretty long-winded which is why I don't want to repeat it for each case in the switch statement. I've seen something on ScottGu's blog about a framework but it seemed a bit much for what I needed.

Thanks,
J
///Non Dynamic Way

switch(sFilterProperty.ToLower())

{

case "number of seats":

var V1 = (from r in myList group r by (r.NmbSeats));

///MORE STUFF TO DO

break;

 

case "number of doors":

var V1 = (from r in myList group r by (r.NmbDoors));

///MORE STUFF TO DO

break;

 

default:

break;

}
 

//I'D LIKE SOMETHING LIKE 

{

     var property = ?????

    switch(sFilterProperty.ToLower())

     {

       case "number of seats":

       property = car.NmbSeats;

        break;

 

       case "number of doors":

       property =car.NmbDoors;

       break;

 

default:

break;

}
 

var MyGrouped List = (from r in myList group r by r.$property$);

///MORE STUFF TO DO
 

}

Open in new window

0
Comment
Question by:Jobbietronic
1 Comment
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 24029360
Hi Jobbietronic;

This type of query, var MyGrouped List = (from r in myList group r by r.$property$);, is not possible in standard Linq query because .Net languages are strongly typed and it needs to know what property $property$ is in r.$property$. Microsoft has build a Library for Linq called Dynamic LINQ which will allow you to use a string as the query arguments to the Linq methods. An explanation of the Library use and where to download it from can be found on Scott Guthrie Blog at
 http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
Otherwise you are left with writing a query for each possibility.

Fernando
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
C# Export DataGridView 4 38
Paging GridView 7 32
c# if statement weird reaction 3 29
Visual Studio editor? 1 12
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now