Jonas Stawski
Jonas Stawski

Reputation: 6752

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

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?

Upvotes: 5

Views: 319

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062865

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'
               ...
}

Upvotes: 2

Related Questions