c#编译查询

时间:2013-03-18 10:46:18

标签: c# compiled-query

我正在尝试按照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&gt;)'中用作类型参数'TArg0' 。没有从'W.Models.newTestDBContext'到'System.Data.Objects.ObjectContext'的隐式引用转换。

1 个答案:

答案 0 :(得分:0)

我会在这里猜测并建议你的newTestDBContext不继承ObjectContext,或者你传递了错误的对象?

您使用的是Entity框架吗?

您是否尝试过使用本页底部的参考代码,将Adventureworks替换为您自己的架构?

http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.aspx

希望这有帮助!