【问题标题】:LINQ to SQL SOUNDEX - possible?LINQ to SQL SOUNDEX - 可能吗?
【发布时间】:2010-06-07 20:55:25
【问题描述】:

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

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

【问题讨论】:

    标签: sql linq-to-sql soundex


    【解决方案1】:

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

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

    您可以使用如下表达式:

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

    最初的想法来自: Random row from Linq to Sql

    【讨论】:

    • 对于那些尝试过更新版本的人:(以及我自己的未来参考)[DbFunction("SqlServer","SOUNDEX")]
    • 对不起,这是一个答案,你没有提供一种方法让用户只做界面
    【解决方案2】:

    如下添加一个udf

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

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 这似乎是对实体框架的引用,而不是 Linq to SQL,这解释了为什么没有投票。
      • @jpierson 真的,我的错。但我发现这个问题正在寻找 EF 解决方案,所以也许它会对某人有所帮助。
      【解决方案4】:

      这正是 Troy Magennis 在“LINQ to Objects Using C# 4.0”中展示的内容。

      编辑:添加示例花絮和说明:作者的示例是针对 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() );
      

      【讨论】:

      • 对于那些没有书的人,你有例子吗?
      • 虽然我感谢 Mystaggue 的回复,但示例或示例链接比我可以购买的书的链接更有益。
      【解决方案5】:

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

      【讨论】:

      • 您能举个例子吗?
      【解决方案6】:

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

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

      【讨论】:

      • 这是 EF,不是 Linq To SQL。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-24
      • 1970-01-01
      • 2010-09-09
      • 1970-01-01
      • 1970-01-01
      • 2011-07-15
      相关资源
      最近更新 更多