?
Solved

Dynamic Group By in Linq

Posted on 2009-03-31
1
Medium Priority
?
1,984 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 1500 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

Major Serverless Shift

Comparison of major players like AWS, Microsoft Azure, IBM Bluemix, and Google Cloud Platform

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

762 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