【发布时间】:2015-02-27 15:33:47
【问题描述】:
我正在尝试使用 LINQ 查询在项目中实现搜索功能。因为数据有时包含带有重音符号和其他符号的字符,所以我创建了一种方法来删除这些以进行搜索。
这是我的代码:
var addresses = (from a in db.Addresses
join b in db.Addresses on a.ClientID equals b.ClientID
where a.AddressType == 1 && b.AddressType == 2
select new
{
/* Columns selected */
});
var q = (from e in db.Employers
join a in addresses on e.EmployerID equals a.id
join i in db.IndustrialSectors on e.IndustrialSector equals i.ID
select new
{
/* Columns selected */
});
if (search != "")
{
q = (from i in q
where (
Util.StringUtil.RemoveDiacritics(i.entity.ToString().ToLowerInvariant()).Contains(search) ||
Util.StringUtil.RemoveDiacritics(i.name.ToString().ToLowerInvariant()).Contains(search)
)
select i);
}
它会生成一个异常,指出 LINQ to Entities 无法识别我的方法 (RemoveDiacritics(String))。
【问题讨论】:
-
LINQ to entity 将尝试将您的 LINQ 表达式转换为 SQL 或基础数据源语言。由于您的 particular 方法没有任何表示,因此它会引发异常。
-
如果您的底层数据源语言是 SQL,您可以在 Entity Framework Linq 查询中使用 SqlFunctions 类中的静态方法
-
您针对哪种 SQL 方言进行编码?如果是 MS SQL Server,您能否将
RemoveDiacritics函数实现为自定义函数,无论是在 T-SQL 中还是作为基于 CLR 的函数?
标签: c# linq entity-framework