相关疑难解决方法(0)

IEnumerable <T>/IQueryable <T>上的动态LINQ OrderBy

我在动态LINQ 的VS2008示例中找到了一个示例,它允许您使用类似sql的字符串(例如,OrderBy("Name, Age DESC"))用于排序.不幸的是,包含的方法仅适用于IQueryable<T>;.有没有办法获得此功能IEnumerable<T>

c# linq linq-to-objects

657
推荐指数
12
解决办法
26万
查看次数

如何动态指定Linq OrderBy参数?

如何指定传递给orderby使用我作为参数的值的参数?

例如:

List<Student> existingStudends = new List<Student>{ new Student {...}, new Student {...}}
Run Code Online (Sandbox Code Playgroud)

目前实施:

List<Student> orderbyAddress = existingStudends.OrderBy(c => c.Address).ToList();
Run Code Online (Sandbox Code Playgroud)

而不是c.Address,我如何将其作为参数?

 string param = "City";
 List<Student> orderbyAddress = existingStudends.OrderByDescending(c => param).ToList();
Run Code Online (Sandbox Code Playgroud)

c# linq

82
推荐指数
6
解决办法
7万
查看次数

如何为运行时排序创建表达式树?

使用Entity Framework 4,我正在尝试基于成员名称的集合实现动态排序.基本上,用户可以选择要排序的字段和排序顺序.我看过表达式树的例子,不能把它拼凑在一起.以下是一些细节:

列名集合:

public List<string> sortColumns;
sortColumns = new List<string>();

/// Example subset of video fields.  The collection will vary.
sortColumns.Add("Width");
sortColumns.Add("Height");
sortColumns.Add("Duration");
sortColumns.Add("Title");
Run Code Online (Sandbox Code Playgroud)

视频类定义如下:

public class Video
{
    public string Title { get; set; }
    public int Width { get; set; }
    public int Height { get; set; }
    public float Duration { get; set; }
    public string Filename { get; set; }
    public DateTime DateCreated { get; set; }
    .
    .
    .
}
public List<Video> Videos;
Run Code Online (Sandbox Code Playgroud)

我想要做的是通过sortColumns集合枚举以在运行时构建表达式树.此外,用户可以指定升序或降序排序,表达式树也应该处理. …

c# lambda entity-framework-4

11
推荐指数
1
解决办法
4345
查看次数

将字符串解析为LINQ查询

将LINQ字符串解析为查询的最佳实践方法是什么?

换句话说,转换最有意义的方法是:

 string query = @"from element in source
                  where element.Property = ""param""
                  select element";
Run Code Online (Sandbox Code Playgroud)

 IEnumerable<Element> = from element in source 
                        where element.Property = "param"
                        select element;
Run Code Online (Sandbox Code Playgroud)

假设source指的是IEnumerable<Element>IQueryable<Element>在本地范围内.

.net c# linq iqueryable linq-query-syntax

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

具有通用memberepressation的多个排序

编辑2019-01-31:最新解决方案

我已经按照这里这里的示例创建了一个带有memberexpressions的泛型排序,但我无法弄清楚我应该如何添加一个"ThenBy"子句,或者在methodcallexpression中组合多个列进行排序.理想情况下,ThenBy应该在跳过之前进行,但它不能,因为它无法看到我使用methodcallexpression创建的orderby子句.GridSortExpression是一个Telerik类 - 它只描述了查询应该排序的列和方向.

任何人都能解释一下吗?这就是我现在所拥有的:

Dim exp As Expressions.Expression(Of Func(Of Product_Catalog, Boolean)) = PredicateBuilder.True(Of Product_Catalog)()
exp = exp.And(Function(e) e.Chapter_Price > 30)
Dim sortExpression As New List(Of GridSortExpression)({New GridSortExpression() With {.SortOrder = GridSortOrder.Descending, .FieldName = "Id"}})
If sortExpression.Count = 0 Then
     catalogList = con.Product_Catalogs.AsExpandable.Where(exp).OrderBy(Function(o) o.Item_Type).ThenBy(Function(o) o.Item_Description).Skip(startRowIndex).Take(maximumRows).ToList
Else
     Dim param As ParameterExpression = Expression.Parameter(GetType(Product_Catalog), String.Empty)
     Dim prop As MemberExpression = Expression.PropertyOrField(param, sortExpression(0).FieldName)
     Dim sort As LambdaExpression = Expression.Lambda(prop, param)
     Dim source = con.Product_Catalogs.AsExpandable.Where(exp)
     Dim resultExp As MethodCallExpression …
Run Code Online (Sandbox Code Playgroud)

linq vb.net asp.net

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

如何使用字符串通过表达式创建EF顺序?

我试图实现这种转换

"Address.Street" => (p) => p.Address.Street
"Name" => (p) => p.Name
Run Code Online (Sandbox Code Playgroud)

我能够找到一种通过表达式使用反射生成订单的方法,但它不适用于复杂排序,Address.Street因为它适用于单个属性级别.

有没有办法做到这一点?我已经看到我编译lambda表达式,但我无法理解如何使它适用于这种情况.

c# linq entity-framework

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

ExpressionTree重写 - 导航属性的MakeMemberAccess()

前一个问题隐约有关

:我使用的ExpressionTree游客的衍生物作为解释这里

在我的VisitMemberAccess方法中,我目前使用以下内容创建MemberExpressions:

// `mapping` is a class used to map EntityA's members to EntityB's members
return Expression.MakeMemberAccess(Visit(m.Expression), mapping.TargetMemberInfo);
Run Code Online (Sandbox Code Playgroud)

在大多数情况下,这是有效的.

鉴于一些测试类......

public class EntityA
{
    public long Id { get; set; }
    public string Name { get; set; }
}

public class EntityB
{
    public long MyId {get; set; }
    public string MyName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

代码将正确映射(EntityA x) => x.Id(EntityB x) => x.MyId哪个是伟大的,并且工作可爱.引入导航属性时出现问题:

public class EntityB
{ …
Run Code Online (Sandbox Code Playgroud)

c# expression-trees .net-3.5

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