实体框架 - 联合导致“无法创建类型的常量值..”
要选择所有处于活动状态的 Scheduling
,我有以下代码:
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
}
(Factory
is an int
)
我运行此代码,在 foreach
行上收到此神秘错误:
无法创建“System.Collections.Generic.IEnumerable`1”类型的常量值。此上下文仅支持原始类型(“例如 Int32、String 和 Guid”)。
奇怪的是,我可以分别枚举 allSchedulesOnALine
和 allSchedulesUnscheduled
。更奇怪的是,如果我重新排序联合:
IEnumerable<Scheduling> allSchedules = allSchedulesOnALine.Union(allSchedulesUnscheduled);
它工作得很好!
有谁知道为什么会发生这种情况?我是否遗漏了一些重要的东西,或者这是一个错误?
我应该提到我正在使用 Entity Framework 3.5。目前 EF4 不是我们的选择 - 它超出了我的控制范围:\
To select all Scheduling
s 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
}
(Factory
is an int
)
When I run this code, I get this cryptic error on the foreach
line:
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.
Strangely enough, I can enumerate both allSchedulesOnALine
and allSchedulesUnscheduled
separately. Even stranger, if I reorder the union:
IEnumerable<Scheduling> allSchedules = allSchedulesOnALine.Union(allSchedulesUnscheduled);
It works fine!
Does anyone have any idea why this would happen? Am I missing something crucial, or is this a bug?
I should mention I am using Entity Framework 3.5. EF4 is not an option for us currently - it is beyond my control :\
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您通过“重新排序”调用两种不同的方法。
您没有显示
allSchedulesOnALine
或allSchedulesUnscheduled
的类型,但我敢打赌allSchedulesOnALine
的类型为IEnumerable< /code> 和
allSchedulesUnscheduled
的类型为IQueryable
。因此,当您调用 Queryable.Union 时,您是在要求 EF 将表达式转换为 SQL 。但是您传递的参数属于
IEnumerable
类型,并且它无法将其转换为查询。另一方面,当您调用 Enumerable.Union 时,您'要求 LINQ to Objects 在内存中完成整个操作,虽然速度可能较慢,但效果很好。
因此,行为不同的原因是您调用了两个完全不同的方法,它们执行不同的操作,但碰巧具有相同的名称。不,这不是一个错误。
You're calling two different methods with your "reordering".
You don't show the types of
allSchedulesOnALine
orallSchedulesUnscheduled
, but I'm bettingallSchedulesOnALine
is of typeIEnumerable<Schedule>
andallSchedulesUnscheduled
is of typeIQueryable<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.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.