Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
LINQ到SQL SOUNDEX-可能吗?_Sql_Linq To Sql_Soundex - Fatal编程技术网

LINQ到SQL SOUNDEX-可能吗?

LINQ到SQL SOUNDEX-可能吗?,sql,linq-to-sql,soundex,Sql,Linq To Sql,Soundex,我对此做了一些研究,并在这里查阅了一些关于StackOverflow的文章以及一些博客文章,但没有找到确切的答案。我还了解到,使用4.0框架是可能的,但尚未找到任何支持证据 所以我的问题是,是否可以通过LINQ到SQL查询执行SOUNDEX?这正是Troy Magennis在“”中演示的内容 编辑:添加示例tid和澄清:作者的示例是针对LINQtoObject而不是LINQtoSQL的。作者只是制作了一个iQualityComparer,其中一些片段看起来像这样 public class Sou

我对此做了一些研究,并在这里查阅了一些关于StackOverflow的文章以及一些博客文章,但没有找到确切的答案。我还了解到,使用4.0框架是可能的,但尚未找到任何支持证据


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

这正是Troy Magennis在“”中演示的内容

编辑:添加示例tid和澄清:作者的示例是针对LINQtoObject而不是LINQtoSQL的。作者只是制作了一个iQualityComparer,其中一些片段看起来像这样

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() );
公共类SoundexEqualityComparer:IEqualityComparer
{
公共布尔等于(字符串x、字符串y)
{
返回GetHashCode(x)==GetHashCode(y);
}
public int GetHashCode(字符串obj)
{
//e、 g.转换soundex代码A123,
//到整数:65123
int结果=0;
字符串s=soundex(obj);
if(string.IsNullOrEmpty=false)
结果=转换为32(s[0])*1000+
转换为32(s.子字符串(1,3));
返回结果;
}
专用字符串soundex(字符串s)
{
//e、 这篇文章省略了这本书的实现。
}
}
//用法示例(假设“名称”中有字符串数组)
var q=names.GroupBy(s=>s,新的SoundexEqualityComparer());

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

添加一个udf,如下所示

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

只需将它从服务器资源管理器拖到visual studio dbml文件中的数据上下文中,并在代码中将其用作datacontext类中公开的方法。

您可以在数据库中使用一个伪UDF来实现这一点;在分部类中,向数据上下文添加方法:

[DbFunction(Name = "SoundEx", IsComposable = true)]
public string SoundsLike(string input)
{
    throw new NotImplementedException();
}
可以使用以下表达式作为表达式:

x => db.SoundsLike(x.QuoteValue) == db.SoundsLike("text")
最初想法来自:

自.net 4以来,这也将起作用:

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

此处的更多信息:

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


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

对于那些没有这本书的人,你有没有一个例子?尽管我感谢你对Mystagogue的回答,一个例子或一个例子的链接比一本我可以购买的书的链接更有益。你能提供一个例子吗?这似乎是对实体框架的引用,而不是对SQL的引用,这解释了为什么没有向上投票。@jpierson True,我的错。但是我在搜索EF解决方案时发现了这个问题,因此可能会对某些人有所帮助。对于那些尝试使用较新版本的人:(以及我自己将来的参考)[DbFunction(“SqlServer”,“SOUNDEX”)]对不起,这是一个什么样的答案?你没有为用户提供一种方法,只是界面这是EF,而不是Linq to SQL。