【发布时间】:2013-03-18 10:46:18
【问题描述】:
我正在尝试按照 http://msdn.microsoft.com/en-us/library/bb896297.aspx 的 MSDN 示例进行编译查询
这是我的代码
static readonly Func<newTestDBContext, string, long, IQueryable<RouteQueryModel>> s_compiledQuery2 =
CompiledQuery.Compile<newTestDBContext, string, long, IQueryable<RouteQueryModel>>(
(db, currentLocation, x) =>
from b in db.routes
let avg_rating = b.ratings.Any() ?
b.ratings.Select(r => r.rating1).Average() :
0
let coorCount = b.coordinates.Count()
let is_favorite = b.favorites.Any(c => c.users_id == x) ?
true :
false
let distance_to_first_from_me = b.coordinates.
Select(c => c.position).
FirstOrDefault().
Distance(DbGeography.FromText(currentLocation, 4326))
let distance_to_last_from_me = b.coordinates.
OrderByDescending(c => c.sequence).
Select(d => d.position).
FirstOrDefault().
Distance(DbGeography.FromText(currentLocation, 4326))
let distance_to_from_me = distance_to_first_from_me < distance_to_last_from_me ?
distance_to_first_from_me :
distance_to_last_from_me
select new RouteQueryModel
{
b = b,
distance_to_from_me = distance_to_from_me.Value,
avg_rating = avg_rating,
coorCount = coorCount,
is_favorite = is_favorite
}
);
我收到以下错误
错误 1 类型“W.Models.newTestDBContext”不能用作泛型类型或方法“System.Data.Objects.CompiledQuery.Compile(System.Linq.Expressions.Expression>)”中的类型参数“TArg0” .没有从“W.Models.newTestDBContext”到“System.Data.Objects.ObjectContext”的隐式引用转换。
【问题讨论】:
-
您使用的是 EF 4 还是 EF 5?您是否禁用了自动查询缓存?
newTestDBContext有哪个基类?
标签: c# compiled-query