作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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/
我是一名优秀的程序员,十分优秀!