Solved

Dynamic Group By in Linq

Posted on 2009-03-31
1
1,909 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 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

22 Experts available now in Live!

Get 1:1 Help Now