LINQ to SQL SOUNDEX - possible? LINQ to SQL SOUNDEX - possible? sql sql

LINQ to SQL SOUNDEX - possible?


You can do this at the database, by using a fake UDF; in a partial class, add a method to the data context:

[DbFunction(Name = "SoundEx", IsComposable = true)]public string SoundsLike(string input){    throw new NotImplementedException();}

You can use as an expression like:

x => db.SoundsLike(x.QuoteValue) == db.SoundsLike("text")

Initial idea from:Random row from Linq to Sql


Add a udf as below

CREATE FUNCTION [dbo].[udfSoundex](    @Soundex nvarchar(100))RETURNS nvarchar(100)ASBEGIN    RETURN Soundex(@Soundex)END

Simply drag it from server explorer onto you data context in the visual studio dbml file and use it in code as a method exposed on your datacontext class..


Since .net 4 this will work as well:

from p in mytablewhere SqlFunctions.SoundCode(p.MyRow) == SqlFunctions.SoundCode("test")select p

More info here: http://msdn.microsoft.com/en-us/library/system.data.objects.sqlclient.sqlfunctions.soundcode.aspx