I am looking for a way to create a function that can be used in my Linq queries that will translate into SQL. When we used Linq-to-SQL, I posed a similar question . The answer was to map the UDF to db. We use the first code model because of the purity of the model definition, but, unfortunately, there is no way to define functions , map functions, and map stored procs, as far as I can tell (I think the idea is that the code should generate db first, and I should not add something to it). It does not seem very scalable to me, because it does not allow you to easily use inefficiency problems that can develop over time, but neither here nor there ...
In any case, I know that I can define predicate expressions that will translate into SQL, which I can use in my queries as follows:
public static Expression<Func<MyEntity, bool>> FilterMe(int comparisonNumber) { return x => x.SomeProperty == comparisonNumber; } var query = MyEntities.Where(FilterMe(1));
Is this only possible for expressions that return a bool
? I am looking for something like:
var query = from m in MyEntities let endDate = GetEndDate(m.Start, m.Duration) where endDate <= DateTime.Now select m;
Is there a way to build GetEndDate
in an expression function? The code in GetEndDate
translates to SQL just fine when I write it for a long time in a query, but it is quite long and confusing to read.
Edit - Oh, and before someone answers โUse SQLFunctions for DateAddโ, my example is just ... an example. There are many other ways that I would like to use. Thanks in advance.
source share