10

私はこれについて少し調査し、StackOverflowに関するいくつかの記事といくつかのブログ投稿を調べましたが、正確な答えは見つかりませんでした。また、4.0フレームワークを使用してそれを行うことは可能であると読みましたが、それを裏付ける証拠はまだ見つかりません。

だから私の質問は、LINQ to SQLクエリを介してSOUNDEXを実行することは可能ですか?

4

6 に答える 6

21

偽のUDFを使用して、データベースでこれを行うことができます。部分クラスで、データコンテキストにメソッドを追加します。

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

次のような式として使用できます。

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

最初のアイデア: LinqからSqlへのランダムな行

于 2010-06-09T16:50:07.097 に答える
6

以下のようにudfを追加します

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

サーバーエクスプローラーからVisualStudioのdbmlファイルのデータコンテキストにドラッグし、データコンテキストクラスで公開されるメソッドとしてコードで使用するだけです。

于 2010-06-07T23:16:50.877 に答える
5

.net 4以降、これも機能します。

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

詳細はこちら: http: //msdn.microsoft.com/en-us/library/system.data.objects.sqlclient.sqlfunctions.soundcode.aspx

于 2013-05-08T07:56:10.163 に答える
2

それはまさに、トロイ・マゲニスによる「C#4.0を使用したオブジェクトへのLINQ 」で示されているものです。

編集:例のtid-bitsと説明の追加:作成者の例は、LINQ to SQLではなく、LINQtoobjectsです。著者は単にIEqualityComparerを作成しましたが、その一部は次のようになりました...

public class SoundexEqualityComparer : IEqualityComparer<string>
{
  public bool Equals(string x, string y)
  {
     return GetHashCode(x) == GetHashCode(y);
  }

  public int GetHashCode(string obj)
  {
     //e.g. convert soundex code A123,
     //to an integer: 65123
     int result = 0;

     string s = soundex(obj);
     if (string.IsNullOrEmpty(s) == false)
        result = Convert.ToInt32(s[0]) * 1000 +
                 Convert.ToInt32(s.Substring(1, 3));
     return result;
  }

  private string soundex(string s)
  {
     //e.g. book's implementation omitted for this post.
  }
}

//example usage (assuming an array of strings in "names")
var q = names.GroupBy(s => s, new SoundexEqualityComparer() );
于 2010-06-07T20:58:42.467 に答える
0

SQL Serverでは、SOUNDEXをUDF(ユーザー定義関数)でラップできます。これをDataContextクラスに追加すると、DataContextを介して使用できるようになります。

于 2010-06-07T20:59:34.210 に答える
0

Soundex関数にマップするSqlFucntions.Differenceメソッドを使用することもできます。

SqlFunctions.Difference(string, string) returns int - the higher the return value, the more "similar" the strings are.

于 2013-08-01T20:12:57.763 に答える