我正在使用EF4。我试图将SQL转换为LINQ到EF,我收到一个错误。我希望EF在数据库中创建和执行SQL(不在内存中执行操作)。比较是使用原始类型,它是一个整数。如何修复错误。我正在考虑将现在的组用于语法。
SQL:
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'
LINQ:(在LinqPad中测试)
var query =
from restaurant in Restaurants
from customerRating
in CustomerRatings
.Where( cr => cr.RestaurantID == restaurant.RestaurantID)
.DefaultIfEmpty()
from foodType
in FoodTypes
.Where(ft => ft.FoodTypeID == customerRating.FoodTypeID && ft.FoodTypeName == "Taco")
.DefaultIfEmpty()
select restaurant;
query.Dump();
错误消息:
Unable to create a constant value of type 'CustomerRating'.
Only primitive types ('such as Int32, String, and Guid') are supported in this context.
答案 0 :(得分:0)
我继续使用加入解决方案。我仍然感兴趣为什么我得到了其他风格的错误。
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();