【问题标题】:The LINQ lambda expression not workingLINQ lambda 表达式不起作用
【发布时间】:2015-05-12 13:54:08
【问题描述】:

我有下面的 Linq 表达式

var orderByLambda = Expression.Lambda<Func<Queue, int>>(
    nullCheckExpression, parameterExpression);

queue = context.Table
    .Join(context.tablea, cq => cq.a, r => r.a, (cq, r) => new { cq, r })
    .Join(context.tableb, s => s.r.b, se => se.b, (s, se) => new { s, se })
    .Join(context.tablec, u => u.s.cq.c, us => us.c, (u, us) => new { u, us })
    .Where(cq => cq.u.s.cq.c == Utilities.Authentication.c)
    .Where(cq => buildStatusOrder.Contains((BuildStatusEnum)cq.u.s.cq.d))
    .OrderBy(o => o.u.se.b)
    .Select(s => new QueueInfo
    {
        x = s.u.c,
        y = s.u.d,
        z = s.u.a
    });

queue = queue.OrderBy(f => orderByLambda);

var concat = queue.GroupBy(e => new { e.x, e.y, e.z })
    .OrderBy(v => v.FirstOrDefault().segmentID)
    .ToList()
    .Select(ss => new QueueInfo
    {
        x = ss.x,
        y = ss.y,
        z = ss.z,
    })
    .AsQueryable();

我在concat 中遇到错误

The LINQ expression node type 'Lambda' is not supported in LINQ to Entities.

我的代码出了什么问题?

【问题讨论】:

  • 您仍然没有按照 Habib 的建议进行修复。这就是导致错误的行。

标签: c# linq lambda expression


【解决方案1】:

代替

queue = queue.OrderBy(f => orderByLambda);

用途:

queue = queue.OrderBy(orderByLambda);

【讨论】:

    【解决方案2】:

    关于您的代码的两个注意事项:

    1. LINQ to Entities (Entity Framework) 想要将您的查询转换为 SQL,因此它只能执行它知道如何转换的操作。例如,不能使用Contains等最常见的LINQ收集方法。
    2. 您已经定义了一个用于排序的 lambda 对象,但没有定义实际表达式所做的 - 或者至少,您还没有向我们展示nullCheckExpressionparameterExpression 是什么。一般来说,这不是您对 LINQ to Entities 进行排序的方式——它应该类似于queue.OrderBy(f =&gt; f.x).ThenBy(f =&gt; f.y);——您必须定义选择中的哪些字段实际用于排序。 (见第 1 点)

    【讨论】:

      猜你喜欢
      • 2019-01-08
      • 1970-01-01
      • 1970-01-01
      • 2020-06-18
      • 2015-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多