Is there a way to call a SQL Server user defined function from LINQ to Entities while using Entity Framework v2?
I’ve found a way to do so using Entity Framework 4 (everything is explained in this blog post, but it appears that the EdmFunctionAttribute class is not available in EF4 🙁
If it is not possible, what is the best way to write a complex where clause in LINQ to entities? Here is an excerpt of my function to show you how complex it is :
IF @ageOfReferenceType = 3 or @ageToCompareType = 3 -- if one is expressed in weeks
BEGIN
--convert both to weeks
SET @ageOfReference = @ageOfReference * CASE @ageOfReferenceType
WHEN 1 THEN 52
WHEN 2 THEN 4
WHEN 3 THEN 1
END
SET @ageToCompare = @ageToCompare * CASE @ageToCompareType
WHEN 1 THEN 52
WHEN 2 THEN 4
WHEN 3 THEN 1
END
END
ELSE -- last solution, one is in years and the other in months
BEGIN
-- convert both to months
SET @ageOfReference = @ageOfReference * CASE @ageOfReferenceType
WHEN 1 THEN 12
WHEN 2 THEN 1
END
SET @ageToCompare = @ageToCompare * CASE @ageToCompareType
WHEN 1 THEN 12
WHEN 2 THEN 1
END
END
END
IF @ageToCompare >= @ageOfReference
BEGIN
RETURN 1
END
It seems there is no solution to my problem, so i’m going to encapsulate the whole linq to entities query into a stored procedure, which will be able to call my UDF.