Solved

Dynamic Group By in Linq

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…

733 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