Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq to SQL - is there a way to map an extension method to a SQL Function/Stored Procedure?

Tags:

c#

linq-to-sql

I have an extension method for the DateTime type that I would like to use in my Linq to Sql. Unfortunately, doing a ToList() and then using the extension method is not an option. Is there a way to map an extension method to an actual SQL Function?

like image 239
Jonas Stawski Avatar asked Nov 06 '22 03:11

Jonas Stawski


1 Answers

Not as an extension method, no; mapped functions must be called as instance functions from the data-context, i.e.

partial class MyDataContext
{
     [Function(Name="MySqlFunctionName", IsComposable=true)] 
     public ReturnType FunctionName(...args...) 
     { ... optional C# impl for AsEnumerable(),
           else throw NotImplementedException... }
}

and used in some form such as:

using(var dc = new MyDataContext(...))
{
     var qry = from ...
               where dc.FunctionName(row.CreationDate) == 'Whatever'
               ...
}
like image 128
Marc Gravell Avatar answered Nov 15 '22 13:11

Marc Gravell