LINQ to SQL SOUNDEX - 可能吗?

发布于 2024-09-04 10:12:58 字数 146 浏览 5 评论 0原文

我对此做了一些研究,并浏览了 StackOverflow 上的一些文章以及一些博客文章,但尚未找到确切的答案。我还了解到可以使用 4.0 框架来做到这一点,但尚未找到任何支持证据。

所以我的问题是,是否可以通过 LINQ to SQL 查询执行 SOUNDEX?

I have done a little bit of research on this and looked through a few articles both here on StackOverflow as well as some blog posts, but haven't found an exact answer. I also read that it is possible to do it using the 4.0 framework, but have yet to find any supporting evidence.

So my question, is it possible to perform SOUNDEX via a LINQ to SQL Query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

左秋 2024-09-11 10:12:59

添加如下 udf

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

只需将其从服务器资源管理器拖到 Visual Studio dbml 文件中的数据上下文上,然后在代码中将其用作数据上下文类上公开的方法即可。

Add a udf as below

CREATE FUNCTION [dbo].[udfSoundex]
(
    @Soundex nvarchar(100)
)
RETURNS nvarchar(100)
AS
BEGIN
    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..

甜心 2024-09-11 10:12:59

从 .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

Since .net 4 this will work as well:

from p in mytable
where 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

地狱即天堂 2024-09-11 10:12:59

这正是“使用 C# 4.0 的 LINQ to Objects” 中演示的内容特洛伊·马根尼斯著。

编辑:添加示例花絮和说明:作者的示例是针对 LINQ to 对象而不是 LINQ to SQL。作者简单地做了一个 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() );

That is precisely something which is demonstrated in "LINQ to Objects Using C# 4.0" by Troy Magennis.

EDIT: Adding example tid-bits and clarification: the author's example is for LINQ to objects rather than LINQ to SQL. The author simply made an IEqualityComparer, some pieces of which looked like this...

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() );
始终不够 2024-09-11 10:12:59

对于使用 EF Core 6.0 和 SQL Server v16 (2022) 的任何人(我尚未使用任何其他版本的 EF 或 SQL Server 进行测试),

[DbFunction(Name = "SOUNDEX", Schema = "SqlServer", IsBuiltIn = true)]
public static string SoundsLike(string query)
{
   throw new NotImplementedException();
}

及其用途 -

context.TableName.Where(x => SoundsLike(x.Name) == SoundsLike(query));

For anyone using EF Core 6.0 with SQL Server v16 (2022) ( I haven't tested with any other versions of EF or SQL Server),

[DbFunction(Name = "SOUNDEX", Schema = "SqlServer", IsBuiltIn = true)]
public static string SoundsLike(string query)
{
   throw new NotImplementedException();
}

And its use -

context.TableName.Where(x => SoundsLike(x.Name) == SoundsLike(query));
骷髅 2024-09-11 10:12:59

您还可以使用 SqlFucntions.Difference 方法,该方法映射到 Soundex 函数:

SqlFunctions.Difference(string, string) 返回 int - 返回值越高,字符串越“相似”。

You can also use the SqlFucntions.Difference method, which maps to the Soundex function:

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

贱人配狗天长地久 2024-09-11 10:12:59

在 SQL Server 上,您可以将 SOUNDEX 包装在 UDF(用户定义函数)中。您可以将其添加到 DataContext 类中,然后您应该能够通过 DataContext 使用它。

On the SQL Server, you can wrap SOUNDEX in a UDF (User-Defined function). You can add that to your DataContext class, and then you should be able to use it through the DataContext.

橘寄 2024-09-11 10:12:58

您可以使用假 UDF 在数据库中执行此操作;在分部类中,向数据上下文添加一个方法:

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

您可以使用如下表达式:

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

最初的想法来自:
从 Linq 到 Sql 的随机行

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文