SqlFunctions wrapper
-
27-10-2019 - |
Question
I want to use SqlFunctions.StringConvert()
in many places in different projects in my solution.
I dont want each project to hold reference to system.data.entity
so I decided to put a wrapper in my Common
project (all other projects has reference to Common
).
How can I write such wrapper? If I am doing:
public static class SqlUtils
{
public static Func<decimal?, string> StringConvert()
{
return x => SqlFunctions.StringConvert(x);
}
public static Func<double?, string> StringConvert()
{
return x => SqlFunctions.StringConvert(x);
}
}
Then I cannot use it like:
query.Where(x => SqlUtils.StringConvert((decimal)x.SerialNumber).Contains(serialNumber));
because entity framework don't know the method SqlUtils.StringConvert.
Any ideas how to do it?
Solution
If you want EF to be able to translate it all down to SQL, you have to more work, see: http://blogs.microsoft.co.il/blogs/gilf/archive/2010/01/01/defining-custom-functions-in-entity-framework.aspx
OTHER TIPS
you can wrap the functions like as follows, but I don't think it meaningful:
public static class SqlUtils
{
public static string StringConvert(double? x)
{
return SqlFunctions.StringConvert(x);
}
public static string StringConvert(decimal? x)
{
return SqlFunctions.StringConvert(x);
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow