【问题标题】:c# Compiled Queriesc# 编译查询
【发布时间】: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


【解决方案1】:

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

你在使用实体框架吗?

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

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

希望这会有所帮助!

【讨论】:

  • 我不确定我应该在哪里以及如何使用它,在我有 CompiledQuery 的文件中?
猜你喜欢
  • 1970-01-01
  • 2015-07-03
  • 2014-05-10
  • 1970-01-01
  • 1970-01-01
  • 2012-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多