Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to convert a SQL which use a user defined scalar-valued function into LINQ.
How can I use the user defined scalar-valued function in LINQ?

My SQL is like:
SQL
select ...
from ATABLE
where binarychk(ATABLE.A, ATABLE.B)


"binarychk" is the user defined scalar-valued function:
SQL
create FUNCTION [dbo].[binarychk] (@bit1 varchar(200), @bit2 varchar(200))  
RETURNS char AS  
BEGIN 
	declare @counter int, @flag char
	select @flag = 'F', @counter = 1
	select @bit2 = substring(@bit2, len(@bit2)-len(@bit1)+1, len(@bit1))
	while  @counter <= len(@bit1) and @flag = 'F'
	begin
		if (substring(@bit1, @counter, 1) = convert(char,(convert(bit, substring(@bit1, @counter, 1))  & convert(bit, substring(@bit2, @counter, 1))  ) ) and substring(@bit1, @counter, 1) = '1'
		   )
		select @flag = 'T'
		select @counter = @counter +1
	end
	return @flag
END
Posted

Example function

SQL
CREATE FUNCTION ReverseCustName(@string varchar(100))
RETURNS varchar(100)
AS
BEGIN
    DECLARE @custName varchar(100)
    -- Implementation left as exercise for users.
    RETURN @custName
END


Then in your c# code
C#
[Function(Name = "dbo.ReverseCustName", IsComposable = true)]
[return: Parameter(DbType = "VarChar(100)")]
public string ReverseCustName([Parameter(Name = "string",
    DbType = "VarChar(100)")] string @string)
{
    return ((string)(this.ExecuteMethodCall(this,
        ((MethodInfo)(MethodInfo.GetCurrentMethod())),
        @string).ReturnValue));
}


Finally you would call your user defined function in code.
C#
from x in exampleTable
select new {x.Column = ReverseCustName(x.Value)};


Please note this is for Linq To SQL

If you referring to EF follow this link
http://stackoverflow.com/questions/5526422/custom-sql-functions-and-code-first-ef-4-1[^]

Please vote if this was the correct answer to your problem.
Regards, Hope you come right.
 
Share this answer
 
v2
Comments
Ares Cheung 9-Feb-12 4:31am    
Where should I put the [Function(Name...)] code?
In my business logic .cs class ? or in the EDM.designer.cs class?

I tried to put the code in my business logic .cs class. It told me that the namespace name 'Function' could not be found.

Anything I missed?
Dean Oliver 10-Feb-12 1:32am    
Place it in your business logic class. click your mouse on [Function] and hit Ctrl + . and this will allow you to resolve your namespace. The namespace is System.Data.Linq.Mapping, The dll is System.Data.Linq (in System.Data.Linq.dll
Where should I put the [Function(Name...)] code?
In my business logic .cs class ? or in the EDM.designer.cs class?

I tried to put the code in my business logic .cs class. It told me that the namespace name 'Function' could not be found.

Anything I missed?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900