Linq To Entities“仅支持原始类型或枚举类型"错误 [英] Linq To Entities 'Only primitive types or enumeration types are supported' Error

查看:28
本文介绍了Linq To Entities“仅支持原始类型或枚举类型"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using LinqPad to test my query. This query works when the LInqPad connection is to my database (LInq to SQL) but it does not work when I change the connection to use my Entity Framework 5 Model.dll. (Linq to Entity). This is in C#.

我有两个表,分别称为 Plan 和 PlanDetails.关系是一个计划到多个计划详细信息.

I have two tables called Plan and PlanDetails. Relationship is one Plan to many 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 吗?

I get this error "NotSupportedException: Unable to create a constant value of type 'Domain.Data.Plan'. Only primitive types or enumeration types are supported in this context." Any ideas why this only works with Linq to SQL?

推荐答案

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

basically it means you are using some complex datatype inside the query for comparison. in your case i suspect from p in this.Plans where p.PlanID == pd.PlanID is the culprit.

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

And it depends on DataProvider. It might work for Sql Data Provider, but not for SqlCE data Provider and so on.

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

what you should do is to convert your this.Plans collection into a primitive type collection containing only the Ids i.e.

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

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

and then use this list inside.

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

这篇关于Linq To Entities“仅支持原始类型或枚举类型"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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