【发布时间】:2012-01-23 18:13:14
【问题描述】:
有些类似问题的答案在我的情况下不起作用。
我来了
无法创建“.Model.featureoptions”类型的常量值。此上下文仅支持原始类型(“例如 Int32、String 和 Guid”)。
使用实体优先,EntityFramework 4.1,MVC3,C# 4。
vehicles 是车辆详细信息表,owners 是车主表。 vehicles 和 owners 是内部连接的,并且有效。
features 表是可选功能的列表,例如天窗、油漆等。featureOptions 是一个功能可用选项的列表。例如油漆可以是“珠光”、“matalic”,天窗可以是“玻璃弹出”、“标题+幻灯片”。
vehicleFeatures 是为车辆选择的选项列表,对于特定功能,车辆可以有零个或一个记录。
在此查询中,feature1 应为 null 或功能的选定值(即所选天窗选项),feature2 应为 null 或不同功能的所选值(即所选油漆选项)
var query = (from v in _entities.vehicles
join o
in _entities.owners
on v.OwnerID equals o.OwnerID
// Some more inner joins
select new
{
// <code snipped >
// o. fields and v. fields
// </ code snipped>
feature1 = (from feature1
in _entities.vehiclefeatures
.Where ( f_1 => f_1.VehicleID == v.VehicleID)
join feature1_fo
in _entities.featureoptions
on feature1.FeatureOptionID equals feature1_fo.FeatureOptionID
join feature1_f
in _entities.features
.Where (bt_f => bt_f.CodeEnum==1)
on feature1_fo.FeatureID equals feature1_f.FeatureID
select new featureoptionsDTO () { Option = feature1_fo.Option }
),
feature2 = (from feature2
in _entities.vehiclefeatures
.Where(f_2 => f_2.VehicleID == v.VehicleID)
join feature2_fo
in _entities.featureoptions
on feature2.FeatureOptionID equals feature2_fo.FeatureOptionID
join feature2_f
in _entities.features
.Where(feature2_f => feature2_f.CodeEnum == 2)
on feature2_fo.FeatureID equals feature2_f.FeatureID
select new featureoptionsDTO() { Option = feature2_fo.Option }
)
}
);
foreach (var vehicle in query) // Exception here
{
}
feature1 = (from ..
和
feature2 = (from ..
导致
无法创建“.Model.featureoptions”类型的常量值。此上下文仅支持原始类型(“例如 Int32、String 和 Guid”)。
我知道 LINQ 正在尝试创建一个实体,我怎样才能让它创建一个匿名(或自己的类)呢?
【问题讨论】:
-
这个项目还处于早期阶段。考虑更改为 LinqToSql,但数据库是 MySQL,所以需要像 LightSpeed link 这样的东西。
-
您的模型中没有导航属性吗?或者你为什么要写这么多显式连接?
标签: c# linq entity-framework left-join