LINQ分组动态 [英] LINQ Grouping dynamically

查看:382
本文介绍了LINQ分组动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个记录类列表,因此用户可以通过动态属性名称选择组行。例如 MenuText ROLENAME ActionName 。然后,我必须执行分组,所以我需要一个通用的方法来处理传递列名分组。

例如:

 公共类菜单
{
  公共字符串MenuText {获取;集;}
  公共字符串角色名{获取;集;}
  公共字符串ActionName {获取;集;}
}

公共类菜单
{
 VAR名单=新的名单,其中,菜单>();
 list.Add(新菜单{MenuText =ABC,角色名=管理,ActionName =XYZ};
 list.Add(新菜单{MenuText =ABC,角色名=管理,ActionName =XYZ};
 list.Add(新菜单{MenuText =ABC1,角色名=管理1,ActionName =XYZ1};
 list.Add(新菜单{MenuText =ABC1,角色名=管理1,ActionName =XYZ1};

  /// COLUMNNAME: - 菜单类的名称(MenuText或角色名或ActionName)

  公开的IEnumerable< IGrouping<字符串,IEnumerable的<菜单>>> GroupMenu(字符串COLUMNNAME)
  {
          //这里我想小组菜单列表的COLUMNNAME
  }
}
 

解决方案

如果你不使用数据库时,你可以使用反射:

 私有静态对象GetPropertyValue(obj对象,字符串propertyName的)
{
    返回obj.GetType()的getProperty(propertyName的).GetValue(OBJ,空)。
}
 

用于为:

  VAR分组= enumeration.GroupBy(X => GetPropertyValue(X,COLUMNNAME));
 

这是一个pretty的原料解决方案,更好的方法应该是使用<一个href="http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx">Dynamic LINQ :

  VAR分组= enumeration.GroupBy(COLUMNNAME,选择);
 

修改动态的LINQ也许需要一些解释。这不是一个技术,一个库或一个全新的框架。这是为让你写这样的查询助手方法一对夫妇(2000 LOC)只是一个方便的名称。只要下载其源(如果你没有安装VS样本),并利用它们在你的code。

I have a class list of records, so user can select to group rows dynamically by property name. For example MenuText, RoleName or ActionName. Then I have to execute grouping so I need a generic method to handle grouping by passing column name.

Example :

public class Menu
{
  public string MenuText {get;set;}
  public string RoleName {get;set;}
  public string ActionName {get;set;}
}

public class Menus 
{
 var list = new List<Menu>();
 list.Add( new Menu {MenuText="abc",RoleName ="Admin", ActionName="xyz"};
 list.Add( new Menu {MenuText="abc",RoleName ="Admin", ActionName="xyz"};
 list.Add( new Menu {MenuText="abc1",RoleName ="Admin1", ActionName="xyz1"};
 list.Add( new Menu {MenuText="abc1",RoleName ="Admin1", ActionName="xyz1"};

  /// columnName :- Name of the Menu class ( MenuText  or RoleName  or ActionName)

  public IEnumerable<IGrouping<string,IEnumerable<Menu>>> GroupMenu(string columnName)
  {
          // Here I want to get group the list of menu by the columnName 
  }
}

解决方案

If you're not working with a database you can use Reflection:

private static object GetPropertyValue(object obj, string propertyName)
{
    return obj.GetType().GetProperty(propertyName).GetValue(obj, null);
}

Used as:

var grouped = enumeration.GroupBy(x => GetPropertyValue(x, columnName));

This is a pretty raw solution, a better way should be to use Dynamic LINQ:

var grouped = enumeration.GroupBy(columnName, selector);

EDIT Dynamic LINQ maybe needs some explanations. It's not a technology, a library or a brand new framework. It's just a convenient name for a couple (2000 LOC) of helpers methods that let you write such queries. Just download their source (if you don't have VS samples installed) and use them in your code.

这篇关于LINQ分组动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆