tags:

views:

153

answers:

3

I have a sql statement that uses Difference => http://msdn.microsoft.com/en-us/library/ms188753.aspx

I do this in a stored proc but i want to change to LINQ. Is there an equal to Difference in LINQ?

Ex: WHERE (DIFFERENCE(C.LastName, ''' + @name + ''') >= 4

Thanks.

A: 

No, there's no .NET method that translates to that.

Mehrdad Afshari
Yes, there is. :)
Adam Sills
As of .NET 3.5, there isn't one for LINQ to SQL. The one you mentioned clearly specify two things: 1. LINQ to Entities. 2. Supported in 4.
Mehrdad Afshari
Indeed. Was thinking of SqlMethods.[XXX] which doesn't have DIFFERENCE. Luckily there is an easy way to expose what he needs though.
Adam Sills
A: 

.NET doesn't have Soundex functionality built-in. See this article on how to implement Soundex in C# including the algorithm that T-SQL's DIFFERENCE() function uses.

Talljoe
+1  A: 

The System.Data.Objects.SqlFunctions has (AFAIK) all the useful T-SQL functions like Difference, Soundex, Like, etc. These functions are translated in where clauses by Linq2Sql into their T-SQL equivalents.

EDIT: Whoops, that's 4.0. Sorry about that. That said, to do so you can create a user-defined function in your database, and add that function to your DBML file and call it just like you would SqlMethods.Like (which is the one I was thinking of). Here's the MSDN docs on it.

Basically you just create a UDF that takes the same inputs as DIFFERENCE and returns the same results. That function does nothing but call the DIFFERENCE method. Then you expose that in your DBML, and use your function in your queries.

Adam Sills