C# 实体框架错误:无法创建类型为';的常量值';。只有基元类型
我正在使用EF4。我正在尝试将SQL转换为LINQ到EF,但遇到了一个错误。我希望EF在数据库中创建和执行SQL(而不是在内存中执行)。比较使用的是一个整数基元类型。如何修复错误。我正在考虑使用now group转换为语法 SQL:C# 实体框架错误:无法创建类型为';的常量值';。只有基元类型,c#,.net,entity-framework,linq-to-entities,C#,.net,Entity Framework,Linq To Entities,我正在使用EF4。我正在尝试将SQL转换为LINQ到EF,但遇到了一个错误。我希望EF在数据库中创建和执行SQL(而不是在内存中执行)。比较使用的是一个整数基元类型。如何修复错误。我正在考虑使用now group转换为语法 SQL: SELECT Restaurant.RestaurantID ,CASE WHEN FoodType.FoodTypeID IS NULL THEN NULL ELSE CustRating.Rating END AS Rating FRO
SELECT Restaurant.RestaurantID
,CASE WHEN FoodType.FoodTypeID IS NULL THEN NULL ELSE CustRating.Rating END AS Rating
FROM Restaurant Restaurant
LEFT OUTER JOIN CustRating CustRating
ON Restaurant.RestaurantID = CustRating.RestaurantID
LEFT OUTER JOIN FoodType FoodType
ON FoodType.FoodTypeID = CustRating.FoodTypeID
AND FoodType.FoodTypeName = 'Taco'
Unable to create a constant value of type 'CustomerRating'.
Only primitive types ('such as Int32, String, and Guid') are supported in this context.
LINQ:(在LinqPad中测试)
错误消息:
SELECT Restaurant.RestaurantID
,CASE WHEN FoodType.FoodTypeID IS NULL THEN NULL ELSE CustRating.Rating END AS Rating
FROM Restaurant Restaurant
LEFT OUTER JOIN CustRating CustRating
ON Restaurant.RestaurantID = CustRating.RestaurantID
LEFT OUTER JOIN FoodType FoodType
ON FoodType.FoodTypeID = CustRating.FoodTypeID
AND FoodType.FoodTypeName = 'Taco'
Unable to create a constant value of type 'CustomerRating'.
Only primitive types ('such as Int32, String, and Guid') are supported in this context.
我继续加入了一个解决方案。我仍然感兴趣的是,为什么我在另一种风格上会出现错误
var query = from restaurant in Restaurants
join cr in CustomerRatings on restaurant.RestaurantID equals cr.RestaurantID
into children
from child in children.DefaultIfEmpty()
join foodType in FoodTypes
on child.FoodTypeID equals foodType.FoodTypeID
into children2
from child2 in children2.Where(ft => ft.FoodTypeName == "Taco").DefaultIfEmpty()
select new { RestaurantID = restaurant.RestaurantID,
FoodTypeID = (int?) child2.FoodTypeID,
Rating = ( (int?) child2.FoodTypeID == null? 0 : (int?)child.Rating )
};
query.Dump();
你最初的方法是错误的。这不是你所期望的。即使它可以工作,您也会得到带有两个交叉联接的查询。