2

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?

Mladen Macanović
  • 1,099
  • 7
  • 17

1 Answers1

4

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.

IT.
  • 859
  • 4
  • 11
  • thank you for the answer but what I need to write inside this method body to return the value from server function? – Mladen Macanović Jul 08 '11 at 07:22
  • Nothing. The example is complete. The SqlFunction attribute tells BLToolkit not to call GetTax, but convert it to SQL. – IT. Jul 08 '11 at 16:59
  • heh, who would it think it was that simple, than you :) PS In case you didn't saw, I have posted you an issue on GitHub a few days ago. – Mladen Macanović Jul 09 '11 at 10:02