小编ine*_*e p的帖子

LINQ动态分组

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

示例:

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)

.net c# linq group-by

11
推荐指数
3
解决办法
2万
查看次数

从Int到string的LINQ表达式转换/ Concat

我想将Concat的两个表达式用于最终表达式

Expression<Func<T, string>>
Run Code Online (Sandbox Code Playgroud)

所以我创建了表达式belwo代码只适用于字符串类型,如果我将memberExpression作为Int32或DateTime抛出异常

类型'System.Int32'的表达式不能用于方法'System.String Concat(System.String,System.String)'的'System.String'类型的参数

如果我将表达式转换为

var conversion = Expression.Convert(memberExpression, typeof (string));
Run Code Online (Sandbox Code Playgroud)

获取没有强制运算符是在类型'System.Int32'和'System.String'之间定义的.

请帮我解决

MethodInfo bodyContactMethod = typeof (string).GetMethod("Concat",new[] {typeof (string), typeof (string)});

ParameterExpression parameter = Expression.Parameter(typeof (T));

body = Expression.Call(bodyContactMethod, cons, memberExpression);

return Expression.Lambda<Func<T, string>>(body, parameter);
Run Code Online (Sandbox Code Playgroud)

c# linq asp.net c#-4.0 asp.net-mvc-4

7
推荐指数
1
解决办法
6487
查看次数

可移植类库的实体框架

我正在尝试为可移植类库创建Repository,Entity框架,当我尝试Nuget EntityFramework时

它无法添加对'System.ComponentModel.DataAnnotations'的引用.请确保它在全局程序集缓存中.

有任何想法为EF解决这个问题

适用于便携式库的兼容EF包

c# silverlight entity-framework windows-8 windows-phone-8

7
推荐指数
1
解决办法
6239
查看次数

SignalR取代Message Queue

SignalR是否替换MSMQ或IMB MQ或Tibco消息队列.

我已经完成了SignalR.StockTicker

如果我们扩展功能以从多个数据源读取Stock tickers并显示到UI,那么lit会替换Message Queue的使用

.net c# jquery message-queue signalr

6
推荐指数
1
解决办法
5109
查看次数

使用表达式实现LINQ过滤器

在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)

c# linq

5
推荐指数
2
解决办法
3825
查看次数

使用通用LINQ表达式为包含方法调用忽略大小写

我在下面的代码中使用Generic Filter,传递了任何搜索文本,但contains方法是区分大小写的,我怎样才能写入忽略大小写.

public static class QueryExtensions
{
    public static IQueryable<T> Filter<T>(this IQueryable<T> query, string search)    
    {           
        var properties = typeof(T).GetProperties().Where(p => 
                /*p.GetCustomAttributes(typeof(System.Data.Objects.DataClasses.EdmScalarPropertyAttribute),true).Any() && */
                p.PropertyType == typeof(String));        

        var predicate = PredicateBuilder.False<T>();
        foreach (var property in properties )
        {
           predicate = predicate.Or(CreateLike<T>(property,search));
        }
        return query.AsExpandable().Where(predicate);
    }
    private static Expression<Func<T,bool>> CreateLike<T>( PropertyInfo prop, string value)
    {       
        var parameter = Expression.Parameter(typeof(T), "f");
        var propertyAccess = Expression.MakeMemberAccess(parameter, prop);                    
        var like = Expression.Call(propertyAccess, "Contains", null, Expression.Constant(value,typeof(string)));

        return Expression.Lambda<Func<T, bool>>(like, parameter);       
    }

}
Run Code Online (Sandbox Code Playgroud)

.net c# linq linqkit

4
推荐指数
1
解决办法
5254
查看次数

Linq IQueryable 通用过滤器

我正在为任何列/字段映射的查询中的 searchText 寻找通用过滤器

public static IQueryable<T> Filter<T>(this IQueryable<T> source, string searchTerm)
{
   var propNames = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(e=>e.PropertyType == typeof(String)).Select(x => x.Name).ToArray();


 //I am getting the property names but How can I create Expression for

 source.Where(Expression)
}
Run Code Online (Sandbox Code Playgroud)

在这里我给你一个示例场景

现在从我在 Asp.net MVC4 中的 HTML5 表中,我提供了一个搜索框来过滤输入文本的结果,它可以匹配以下任何列/菜单类属性值,我想在服务器端进行此搜索,如何我可以实施吗。

EF 模型类

 public partial class Menu
    {
        public int Id { get; set; }
        public string MenuText { get; set; }
        public string ActionName { get; set; }
        public string ControllerName { get; set; }
        public …
Run Code Online (Sandbox Code Playgroud)

c# linq entity-framework c#-4.0

3
推荐指数
1
解决办法
5331
查看次数

按表达式树的多列组

根据参考资产的LINQ表达式, 我已经实施了Group By Extension,感谢Daniel Hilgarth的帮助,我需要帮助来扩展GroupByMany,如下所示

._unitOfWork.MenuSetRepository.Get()的GroupBy( "Role.Name", "MenuText");

扩展方法

public static IEnumerable<IGrouping<string, TElement>> GroupBy<TElement>(this IEnumerable<TElement> elements,string property)
    {
        var parameter = Expression.Parameter(typeof(TElement), "groupCol");
        Expression<Func<TElement, string>> lambda;
        if (property.Split('.').Count() > 1)
        {
            Expression body = null;
            foreach (var propertyName in property.Split('.'))
            {
                Expression instance = body;
                if (body == null)
                    instance = parameter;
                body = Expression.Property(instance, propertyName);
            }
            lambda = Expression.Lambda<Func<TElement, string>>(body, parameter);
        }
        else
        {
            var menuProperty = Expression.PropertyOrField(parameter, property);
            lambda = Expression.Lambda<Func<TElement, string>>(menuProperty, parameter);    
        }

        var selector= lambda.Compile(); …
Run Code Online (Sandbox Code Playgroud)

c# linq group-by

2
推荐指数
1
解决办法
3122
查看次数