本文介绍了许多人使用linq进行可选的动态分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我遇到动态分组问题。 我有一个要求,按钮可以拖动分组以生成报告 我尝试使用 dynamic linq 。我不想使用魔法字符串。 所以我决定花更多时间在不使用字符串的情况下获得一些东西。 我发现 C#6.0 in a Nutshell - PredicateBuilder [ ^ ]和 其他扩展 c#3.0 - 如何使用LINQ对数据进行分层分组? - 堆栈溢出 [ ^ ] 但扩展中的GroupByMany具有委托作为函数而不表达。 所以我不能尝试像< pre lang = c#>< pre lang = c#>< pre lang = text > foreach ( var 条目 resultEntries) { if (projectGroup ) predicate1 = predicate.And(p = > p.Project.Id == e ntry.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) ; 例如:一个场景的硬编码类型看起来像 var nestedGrouping = reportTimeEntries .GroupByMany(t = > t .Project.Id,t = > t.Client.Id); 我希望这是因为我不知道过滤器被分组发送的顺序。 请帮助我。我在过去的三周内陷入困境。 我尝试过的事情: 公共enum eEntry { NotSet = 0, [描述=客户] 客户= 1, [ Description =Projects] Projects = 2, [Description =Users] Users = 3 } public void SubmitReport(EntryFilter filter){//过滤器包含reportGroups和日期范围 var reportGroups =Projects,Clients ; //实际应该来自filter.But我显示一个场景 //注意:reportGroups可以是1.Clientslients, // 2.Projects, // 3.Users, // 4.Clients,Projects, // 5.Projects,Clients // 6.客户,用户, // 7.用户,客户, // .....等所有可能的组合,包括可选 //以上用于GROUPING MY ENTRIES List< entry> entries = services.getEntries(); //我想按报告组的ID进行分组 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); //和所有其他组合可能 } 解决方案 请,检查一下: Linq GroupByMany动态 - Mitsu的博客 [ ^ ] Hi,I am facing a problem with dynamic grouping.I have a requirement where buttons are draggable for grouping for generating reportI 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 <pre lang="c#"><pre lang="c#"><pre lang="text">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 likevar 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 rangevar 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 ENTRIESList<entry> entries=services.getEntries();//I want to group by IDs of reportGroupsvar 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} 解决方案 Please, check this: Linq GroupByMany dynamically – Mitsu's blog[^] 这篇关于许多人使用linq进行可选的动态分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 05:50