0

I'm trying to make a compiled query, following the MSDN example at http://msdn.microsoft.com/en-us/library/bb896297.aspx

Here is my code

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 
    }
);

I'm getting the following error

Error 1 The type 'W.Models.newTestDBContext' cannot be used as type parameter 'TArg0' in the generic type or method 'System.Data.Objects.CompiledQuery.Compile(System.Linq.Expressions.Expression>)'. There is no implicit reference conversion from 'W.Models.newTestDBContext' to 'System.Data.Objects.ObjectContext'.

RB.
  • 36,301
  • 12
  • 91
  • 131
Lord Vermillion
  • 5,264
  • 20
  • 69
  • 109

1 Answers1

0

I'm going to guess here and suggest that your newTestDBContext doesn't inherit ObjectContext, or you're passing the wrong object?

Are you using Entity framework ?

Have you tried using the reference code at the bottom of this page, replacing adventureworks with your own schema ?

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

Hope this helps!

Russ Clarke
  • 17,511
  • 4
  • 41
  • 45