c# - Entity Framework 6 - Extension Methods for DbFunctions -


i unsure if possible yet, diving entity framework 6. migrating away linq2sql, have many user based queries in our engine. dynamically compile code based on user requirements, , have maintain backward compatibility.

an example of dynamic query passed in legacy linq2sql may looks (stripped down, sample):

from item in context.sometableorview    let sub = sub1 in context.sometableorview    sub1.datatimestamp > item.datatimestamp.addminutes(60) &&   sub1.datatimestamp < item.datatimestamp.addminutes(300)  select possub1  sub.take(1).any() select item 

of course, entity framework not have kind of .addminutes(x) support, , must use new dbfunctions static methods. cant break code, must retrofit it. first solution comes mind, replace text has .addminutes(x), or .addseconds(x), or whatever around datetime, , replace new functions , done it. pre-compile, techincally work. suck @ regular expressions. gladly accept answer if knows how though.

but understand how entityframework works regards extension methods. since return of datetime.addminutes(x) returns new datetime, instead create extension method return expression equivalent of dbfunctions.addminutes(time, increment) or creative that? dbfunctions static, cant return func.

thoughts / suggestions. thanks!

updated - ivan gives great updated answer looking @ this. less error prone below answer, , quite slick imo. enter link description here

entity framework 6 stupid in case. try associated [dbfunction] attribute method, , replaces method dbexpression using system.data.entity.core.objects.elinq.expressionconverter.translator private class. , so, there no way register custom translator outside code. also, .net not offer ability attach attributes language constructs dynamically.

so, solve issue, can 1 of following:

  • replace methods calls methods of dbfunctions class in source code (resharper ssr this);
  • implement expressionvisitor (and possible iqueryprovider) replace method calls methods of dbfunctions class.
  • implement expressionvisitor (and possible iqueryprovider) translate method call expressions dbfunctionexpression.

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -