Solved

Dynamic Group By in Linq

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cant save 3D 4 19
C# bracket error 3 30
Problem!!! 8 33
Get distinct values from excel or comm seperated file 4 23
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

815 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

12 Experts available now in Live!

Get 1:1 Help Now