C# define LET in LINQ -


i have multiple linq queries uses same let variables, predefine these somehow.

iqueryable<routequerymodel> query =     (from b in db.routes      let avg_rating = b.ratings.any() ?           b.ratings.select(r => r.rating1).average() :           0      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      b.endpoints.any(e => values.any(t => t == e.town.town_id))      select new routequerymodel       {           b = b,           distance_to_from_me = distance_to_from_me.value,           avg_rating = avg_rating      }  ); 

i'm using 3 distance_to lets in 8 different queries, there way make template can use in queries?

there easy way pre-compile linq queries:

var distancetofirstfromme =     compiledquery.compile<route, geocoordinates, distance>((route, currentlocation) => {         return route.coordinates             .select(c => c.position)             .firstordefault()             .distance(dbgeography.fromtext(currentlocation, 4326));     }); 

to use them in query, can call them:

iqueryable<routequerymodel> query =     (from b in db.routes      let avg_rating = b.ratings.any() ?           b.ratings.select(r => r.rating1).average() : 0      let distance_to_first_from_me = distancetofirstfromme(b, currentlocation)      // ... 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -