【问题标题】:Entity Framework. Where clause with parameter from query as variable实体框架。将查询中的参数作为变量的 Where 子句
【发布时间】:2016-02-25 07:56:44
【问题描述】:

任务:在一个查询中使用不同的 where 子句

这里是例子(不是真正的查询,只是为了说明问题)

var events = ctx.Events; // ctx - EntityFramework context
var res = events
    .GroupBy(ee => ee.State)
    .Select(gg => new
    {
        State = gg.Key,
        FirstTwo = events
            // how to get this clause from variable
            .Where(ee => ee.State == gg.Key) 
            .Take(2)
    })
    .ToList();

下一个代码不起作用,问题是 where 表达式使用查询 gg.Key 中的参数

var events = ctx.Events;
var res = events
    .GroupBy(ee => ee.State)
    .Select(gg => new
    {
        State = gg.Key,
        FirstTwo = events
            // 1
            // how to get this clause from variable
            //.Where(ee => ee.State == gg.Key)

            // 2
            // try to take out where expression from query
            .Where(_buildExpression(gg.Key))
            .Take(2)
    })
    .ToList();

// method
static Expression<Func<Event, bool>> _buildExpression(string state)
{
    return ee => ee.State == state;
}

// exeption
An unhandled exception of type 'System.InvalidOperationException' occurred in EntityFramework.SqlServer.dll
Additional information: variable 'gg' of type 'System.Linq.IGrouping`2[System.String,Entities.Event]' referenced from scope '', but it is not defined

从变量中获取 where 表达式的示例,但不依赖于 gg.Key(错误)

Expression<Func<Event, bool>> whereClause = (ee) => (ee.State == "test");

var events = ctx.Events;
var res = events
    .GroupBy(ee => ee.State)
    .Select(gg => new
    {
        State = gg.Key,
        FirstTwo = events

            // 1
            // how to get this clause from variable
            //.Where(ee => ee.State == gg.Key)

            // 2
            // try to take out where expression from query
            //.Where(_buildExpression(gg.Key))

            // 3
            // whereClause from variable, but does not depend on gg.Key
            .Where(whereClause)
            .Take(2)
    })
    .ToList();

如何根据 gg.Key 从变量中获取 where сlause?

附言查询只是问题的一个例子。下面的代码并没有解决真实查询的问题:

var events = ctx.Events;
var res = events
    .GroupBy(ee => ee.State)
    .Select(gg => new
    {
        State = gg.Key,
        FirstTwo = gg.Take(2)
    })
    .ToList();

【问题讨论】:

  • 普通代码无法做到这一点。但请查看LinqKit,它可能会有所帮助。
  • 如果您询问如何编写与实际查询等效的 LINQ,您将得到正确答案。 FirstTwo = gg.Take(2) 与您想要使用 Where 子句实现的目标完全相同。如果您得到错误的结果,那么您将无法理解您的要求。您可能还需要其他东西,请说明您的要求。

标签: entity-framework linq linq-to-entities expression


【解决方案1】:

OP 的解决方案。

感谢 Ivan Stoev 的评论。

Expression<Func<Event, string, bool>> whereClause = (ee, state) => (ee.State == state);

var events = ctx.Events;
var res = events
    .AsExpandable() // <= add this
    .GroupBy(ee => ee.State)
    .Select(gg => new
    {
        State = gg.Key,
        FirstTwo = events
            .Where(ee => whereClause.Invoke(ee, gg.Key)) // <= Invoke expression
            .Take(2)
    })
    .ToList();

LinqKit 使这成为可能

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 2011-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多