实体框架 - 联盟导致“无法创建类型的常量值”。 [英] Entity Framework - Union causes "Unable to create a constant value of type.."

查看:73
本文介绍了实体框架 - 联盟导致“无法创建类型的常量值”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要选择所有活动的计划,我有以下代码:

To select all Schedulings which are active, I have the following code:

var allSchedulesOnALine = CurrentUser.Lines.SelectMany(o => o.Scheduling).Where(o => o.Active);
var allSchedulesUnscheduled = Entities.SchedulingSet
    .Where(o => o.Line == null && o.Site.Id == CurrentUser.Site.Id &&
           o.Factory == CurrentUser.Factory && o.Active);

IEnumerable<Scheduling> allSchedules = allSchedulesUnscheduled.Union(allSchedulesOnALine);
foreach(Scheduling schedule in allSchedules.OrderBy(o => o.Ordering))
{
    //Do Stuff
}

工厂是一个 int

(Factory is an int)

当我运行这段代码时,我在 foreach 行:

When I run this code, I get this cryptic error on the foreach line:


无法创建类型为System.Collections.Generic.IEnumerable`1的常量值。在这种情况下,只支持原始类型(如Int32,String和Guid)。

Unable to create a constant value of type 'System.Collections.Generic.IEnumerable`1'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.

奇怪的是,我可以枚举 allSchedulesOnaline allSchedulesUnscheduled 。甚至是陌生人,如果我重新排序工会:

Strangely enough, I can enumerate both allSchedulesOnALine and allSchedulesUnscheduled separately. Even stranger, if I reorder the union:

IEnumerable<Scheduling> allSchedules = allSchedulesOnALine.Union(allSchedulesUnscheduled);

它工作正常!

有没有人有什么想法为什么会发生这种情况?我错过了一些关键的事情,还是这个bug?

Does anyone have any idea why this would happen? Am I missing something crucial, or is this a bug?

我应该提到我正在使用Entity Framework 3.5。 EF4目前不是我们的选择 - 这是我无法控制的:\

推荐答案

两种不同的方法与你的重新排序。

You're calling two different methods with your "reordering".

您不显示 allSchedulesOnaline allSchedulesUnscheduled ,但是我打赌 allSchedulesOnaline 的类型为 IEnumerable< Schedule> allSchedulesUnscheduled 的类型为 IQueryable< Schedule>

You don't show the types of allSchedulesOnALine or allSchedulesUnscheduled, but I'm betting allSchedulesOnALine is of type IEnumerable<Schedule> and allSchedulesUnscheduled is of type IQueryable<Schedule>.

所以当你打电话=http://msdn.microsoft.com/en-us/library/bb156049.aspx> Queryable.Union ,您要求EF将表达式转换为SQL。但您传递的参数的类型为 IEnumerable< Schedule> ,并且无法将其转换为查询。

So when you call Queryable.Union, you're asking the EF to translate the expression into SQL. But the argument you pass is of type IEnumerable<Schedule>, and it can't translate that into a query.

另一方面,当您致电可枚举.Union ,你要求LINQ to Object做内存中的整个事情,尽管可能比较慢。

On the other hand, when you call Enumerable.Union, you're asking LINQ to Objects to do the whole thing in memory, which works fine, albeit perhaps slower.

所以行为的原因不同之处在于,您正在调用两种完全不同的方法,它们做出不同的事情,但是碰巧具有相同的名称。不,这不是一个错误。

So the reason the behavior is different is that you're calling two completely different methods, which do different things, but happen to have the same name. No, it's not a bug.

这篇关于实体框架 - 联盟导致“无法创建类型的常量值”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆