gpt4 book ai didi

Linq To 实体 'Only primitive types or enumeration types are supported' 错误

转载 作者:行者123 更新时间:2023-12-02 01:50:17 25 4
gpt4 key购买 nike

我正在使用 LinqPad 来测试我的查询。当 LInqPad 连接到我的数据库(LInq 到 SQL)时,此查询有效,但当我更改连接以使用 Entity Framework 5 Model.dll 时,此查询不起作用。 (Linq 到实体)。这是用 C# 编写的。

我有两个名为 Plan 和 PlanDetails 的表。关系是一个计划与多个计划详细信息的关系。

var q = from pd in PlanDetails
select new {
pd.PlanDetailID,
ThePlanName = (from p in this.Plans
where p.PlanID == pd.PlanID
select p.PlanName)
};
var results = q.ToList();
q.Dump(); //This is a linqpad method to output the result.

我收到此错误“NotSupportedException:无法创建“Domain.Data.Plan”类型的常量值。此上下文中仅支持原始类型或枚举类型。”知道为什么这只适用于 Linq to SQL 吗?

最佳答案

基本上,这意味着您在查询中使用一些复杂的数据类型进行比较。在你的情况下,我怀疑 from p in this.Plans 其中 p.PlanID == pd.PlanID 是罪魁祸首。

这取决于 DataProvider。它可能适用于 Sql Data Provider,但不适用于 SqlCE data Provider 等。

您应该做的是将 this.Plans 集合转换为仅包含 Id 的原始类型集合,即

var integers = PlanDetails.Plans.Select(s=>s.Id).ToList();

然后在里面使用这个列表。

var q = from pd in PlanDetails
select new {
pd.PlanDetailID,
ThePlanName = (from p in integers
where p == pd.PlanID
select pd.PlanName)
};

关于Linq To 实体 'Only primitive types or enumeration types are supported' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15592042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com