【发布时间】:2012-01-05 23:33:57
【问题描述】:
我正在使用 EF4。我正在尝试将 SQL 转换为 LINQ 到 EF,但出现错误。我希望 EF 在数据库中创建和执行 SQL(而不是在内存中做事)。比较使用的是整数的原始类型。如何修复错误。我正在考虑使用 now group into 语法。
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.
【问题讨论】:
标签: c# .net entity-framework linq-to-entities