Search code examples
linqscalarbltoolkit

Scalar functions using BLToolkit and Linq


I'm in the process of converting my SQL procedures to Linq using the BLToolkit library.

Now I'm stuck on a little problem so if anyone know is this possible I will really appreciate the help.

Anyway I have the sql query that looks something like this (NOTE: this is not my actual table names, i just translated them so you guys can understand what's going on)

select
     P.ProductsID,
     P.Name,
     P.Description,
     dbo.GetTax(P.TaxID, getdate()) -- this is what I need help with
from
     Products P

Nothing too fancy here except that I need to get the tax for every product on current day.

Now I need to convert this to Linq, but I don't know is this even possible. I tried something like this

from p in Products
select new
{
    p.ProductsID,
    p.Name,
    p.Description,
    GetTax(p.TaxID, DateTime.Today) //this is what I need help with
}

and it doesn't work. GetWork function is essentialy just another linq query that returns the tax. I don't know how to call the scallar function using BLToolkit library.

So the question is this. Is it possible to call scalar functions from Linq selected statement?


Solution

  • Define the following function somewhere.

    [SqlFunction(ServerSideOnly=true)]
    public static double GetTax(int id, DateTime dt)
    {
        throw new NotImplementedException();
    }
    

    It will tell BLToolkit to translate GetTax to SQL.