问题描述
我使用LinqPad测试我的查询。当LInqPad连接到我的数据库(LInq到SQL)时,此查询工作,但是当我更改连接以使用我的Entity Framework 5 Model.dll时,它不工作。 (Linq to Entity)。这是在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的表。关系是许多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到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?
推荐答案
基本上它意味着你在查询中使用一些复杂的数据类型进行比较中。
在你的情况下我怀疑从p在这里。其中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数据提供程序,但不适用于SqlCE数据提供程序等。
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'仅支持原始类型或枚举类型'错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!