Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am facing a problem with dynamic grouping.
I have a requirement where buttons are draggable for grouping for generating report

I tried using dynamic linq.I don't want to use magical string.

So I decided to take more time and get something without using strings.

I found C# 6.0 in a Nutshell - PredicateBuilder[^] and
other extension c# 3.0 - How can I hierarchically group data using LINQ? - Stack Overflow[^]

But GroupByMany in extension has delegate as function not expression.

So I could not try like
C#
<pre lang="c#"><pre lang="c#"><pre lang="text">


C#
foreach (var entry in resultEntries)
           {
               if (projectGroup)
                   predicate1 = predicate.And(p => p.Project.Id == entry.Project.Id);

               if (clientGroup)
                   predicate2 = predicate.And(p => p.Client.Id == entry.Client.Id);

               if (userGroup)
                   predicate3 = predicate.And(p => p.User.Id == entry.User.Id);

               predicateResult.And(predicate1);
               predicateResult.And(predicate2);
               predicateResult.And(predicate3);

}
var output  = resultEntries.GroupByMany(predicate);


Ex:Hardcoded type for one scenario looks like
C#
var nestedGrouping = reportTimeEntries
                               .GroupByMany(t => t.Project.Id, t => t.Client.Id);

I want this to be dynamic.Because I don't know the order in which filter are sent for grouping.

Please help me.I am stuck from past three weeks.

What I have tried:

public enum eEntry
{
NotSet = 0,
[Description="Clients"]
Clients = 1,
[Description="Projects"]
Projects = 2,
[Description="Users"]
Users=3
}


public void SubmitReport(EntryFilter filter){//filter contain reportGroups and date range
var reportGroups="Projects,Clients";//actually should come from filter.But I am showing one scenario
//Note: reportGroups can be 1.Clientslients,
// 2.Projects,
// 3.Users,
// 4.Clients,Projects,
// 5.Projects,Clients
// 6.Clients,Users,
// 7.Users,Clients,
// ..... etc. with all possible combinations including optional

//Above are used for GROUPING MY ENTRIES
List<entry> entries=services.getEntries();

//I want to group by IDs of reportGroups
var projectGroup = reportGroups.Contains(eEntry.Projects);
var clientGroup = reportGroups.Contains(eEntry.Clients);
var userGroup = reportGroups.Contains(eEntry.Users);

if (projectGroup)
entries.GroupBy(e=>e.Project.Id);

if (clientGroup)
entries.GroupBy(e=>e.Client.Id);

if (userGroup)
entries.GroupBy(e=>e.User.Id);
// and all other combinations possible

}
Posted
Updated 22-Dec-16 21:56pm

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900