This question is locked. New answers and comments are not allowed.
I've got a SVF that works fine in a standard linq query (var query = from a select b etc.).
I'm trying to construct the linq Expression manually and use a SVF in it. Problem is, the executed query calls the SVF stub (the one that throws new NotImplementedException)
The DebugView of the underlying Expression for both the standard linq and constructed linq look the same.
Is it possible to call scalar valued functions by constructing my own linq Expression?
Some code examples:
var query = Context.Persons.Select(p => new RelevanceResult{ ID = p.PRIMARYKEY, Relevance = ContextClass.GetEditDistance_S(p.Name, search)});
var personQuery = Context.Persons;var parExp = Expression.Parameter(typeof(Person));var newExp = Expression.New(typeof(RelevanceResult));var bindExps = typeof(RelevanceResult).GetProperties().Select(prop => { Expression bindExp = null; if (prop.Name == "ID") { var propExp = Expression.Property(parExp, typeof(Person).GetProperty("PRIMARYKEY")); bindExp = Expression.Call(propExp, typeof(Int32).GetMethod("ToString", new Type[] { })); } else if (prop.Name == "Relevance") { var propExp = Expression.Property(parExp, typeof(Person).GetProperty("Name")); var editDistanceExp = Expression.Call(null, typeof(ContextClass).GetMethod("GetEditDistance_S"), propExp, Expression.Constant(search, typeof(string))); var intToDecExp = Expression.Convert(editDistanceExp, typeof(decimal)); bindExp = intToDecExp; } return Expression.Bind(prop, bindExp); });var lambdaExp = Expression.Lambda(Expression.MemberInit(newExp, bindExps), parExp);// QueryableHelper.SelectGenericMethod is the Queryable.Select<TSource, TResult>(this IQueryable<TSource> source, Expression<Func<TSource, TResult>> selector) generic MethodInfovar query = QueryableHelper.SelectGenericMethod.MakeGenericMethod(typeof(Person), typeof(RelevanceResult)).Invoke(null, new object[] { personQuery, lambdaExp }) as IQueryable<RelevanceResult>;
[MappedFunctionAttribute(Name = "[dbo].[fn_EditDistance]", IsDeterministic = false, Backend = Backend.MsSql)]public static int GetEditDistance_S(string fieldName, string search){ throw new NotImplementedException();}