无法使用LINQ的某些功能实体? [英] Unable to use certain functions with LINQ to Entities?

查看:100
本文介绍了无法使用LINQ的某些功能实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现使用LINQ查询项目中的搜索功能。由于该数据有时会包含重音符号和其他符号的字符,我创建了去除这些用于搜索的方法

I'm trying to implement a search feature on a project using LINQ queries. Because the data sometimes contains characters with accents and other symbols, I created a method to remove these for the search.

下面是我的代码:

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到实体无​​法识别方法(RemoveDiacritics (字符串))。

It generates an exception saying that LINQ to Entities does not recognize my method (RemoveDiacritics(String)).

推荐答案

当您使用LINQ到实体的LINQ查询需要被翻译成服务器 - 方表达,在英文的意思是什么数据库可以执行。该数据库不知道叫你的C#方法什么 RemoveDiacritics 等你拿在运行时出现错误。

When you use LINQ-to-Entities your LINQ query needs to be translated into a "server-side expression", which in English means something that the database can execute. The database doesn't know anything about your C# method called RemoveDiacritics so you get an error at run-time.

你需要先执行查询,然后使用LINQ到对象做的过滤。这可以简单地通过增加一个了ToList()向过滤它之前的查询来完成。我有点比较熟悉的流畅的语法,所以我把它写成:

You will need to execute the query first and then use LINQ-to-Objects to do the filtering. That can be done simply by adding a ToList() to the query prior to filtering it. I'm a little more familiar with the fluent syntax, so I'd write it as:

q.ToList().Where(i =>
    Util.StringUtil.RemoveDiacritics(i.entity.ToString().ToLowerInvariant()).Contains(search) ||
    Util.StringUtil.RemoveDiacritics(i.name.ToString().ToLowerInvariant()).Contains(search));

如果您想查询语法你必须惹它。这可能是像下面这样,但我不是100%肯定。

You'll have to mess with it if you want the query syntax. It might be something like the following but I'm not 100% sure.

(from i in q.ToList()
 where Util.StringUtil.RemoveDiacritics(i.entity.ToString().ToLowerInvariant()).Contains(search) ||
       Util.StringUtil.RemoveDiacritics(i.name.ToString().ToLowerInvariant()).Contains(search));



请注意,但是,这将带回的一切从服务器的然后进行过滤的客户端取决于多少信息包含在你的表这可能导致系统性能的问题。

Be aware, however, that this will bring back everything from the server and then perform the filtering client-side which may cause performance problems in your system depending on how much information is contained in your tables.

这篇关于无法使用LINQ的某些功能实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆