Nip*_*rus 1 c# linq linq-to-sql
我想运行这样的LINQ查询:
var words = from p in db.Words
where p.Document.Corpus.Name == corpus
//where LevenshteinDistance(p.Text.ToCharArray(), word.ToCharArray()) < threshold
select p;
Run Code Online (Sandbox Code Playgroud)
但是,如果我将"LevenshteinDistance"函数放在那里,它将产生一个错误:
NotSupportedException:方法'Char [] ToCharArray()'没有支持的SQL转换.
有没有正确的方法来做到这一点?
LINQ to SQL尝试将整个表达式转换为SQL.如果要在SQL Server上运行距离函数,则需要定义SQL Server UDF并将自定义CLR方法映射到该方法.如果您满足于获取所有结果,然后在距离函数上过滤客户端,请使用AsEnumerable():
var words = (from p in db.Words
where p.Document.Corpus.Name == corpus)
select p)
.AsEnumerable()
.Where(p => /* distance function */ < threshold);
Run Code Online (Sandbox Code Playgroud)
AsEnumerable强制LINQ to SQL枚举查询结果,允许使用LINQ to Objects和距离委托解析剩余的查询(而不是转换为SQL).
| 归档时间: |
|
| 查看次数: |
960 次 |
| 最近记录: |