实体框架 - 联合导致“无法创建类型的常量值..”

发布于 2024-11-06 11:08:17 字数 1197 浏览 0 评论 0原文

要选择所有处于活动状态的 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”)。

奇怪的是,我可以分别枚举 allSchedulesOnALineallSchedulesUnscheduled 。更奇怪的是,如果我重新排序联合:

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

它工作得很好!

有谁知道为什么会发生这种情况?我是否遗漏了一些重要的东西,或者这是一个错误?

我应该提到我正在使用 Entity Framework 3.5。目前 EF4 不是我们的选择 - 它超出了我的控制范围:\

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
}

(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

输什么也不输骨气 2024-11-13 11:08:17

您通过“重新排序”调用两种不同的方法。

您没有显示 allSchedulesOnALineallSchedulesUnscheduled 的类型,但我敢打赌 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 or allSchedulesUnscheduled, but I'm betting allSchedulesOnALine is of type IEnumerable<Schedule> and allSchedulesUnscheduled is of type IQueryable<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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文