我有一个类列表的记录,因此用户可以选择按属性名称动态分组行.例如MenuText,RoleName或ActionName.然后我必须执行分组,所以我需要一个通用方法来通过传递列名来处理分组.
示例:
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 …Run Code Online (Sandbox Code Playgroud) 在MVC4中,我向用户提供搜索框以搜索表中的任何值.所以我在C#中在服务器端实现通用过滤条件
需要帮助来组合多个表达式以形成单个表达式
Expression<Func<T, bool>>
Run Code Online (Sandbox Code Playgroud)
例如
表列
MenuText,角色名称(Role.Name映射),ActionName
现在,如果用户在ABC的搜索框中输入,可以在显示的列中的任何行中,则需要进行过滤.
模型
public class Menu
{
public string MenuText {get;set;}
public Role Role {get;set;}
public string ActionName {get;set;}
}
public class Role
{
public string Name {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止我已经实施了
/// <summary>
/// string[] properties property.Name (MenuText, ActionName), including deeper Mapping names such as (Role.Name)
/// </summary>
public static Expression<Func<T, bool>> FilterKey<T>(string filterText, params string[] properties)
{
ParameterExpression parameter = Expression.Parameter(typeof (T));
Expression[] propertyExpressions = properties.Select(
x => !string.IsNullOrEmpty(x) ? GetDeepPropertyExpression(parameter, x) : null).ToArray(); …Run Code Online (Sandbox Code Playgroud)