获取与LINQ字符最后索引实体 [英] Get last index of character with LINQ to Entities

查看:83
本文介绍了获取与LINQ字符最后索引实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误:

LINQ to Entities does not recognize the method 'Int32 LastIndexOf(System.String)' 
method, and this method cannot be translated into a store expression.

在使用此代码来判断一个人的姓氏与某些字符开头:

When using this code to tell if a person's last name starts with certain characters:

persons = persons.Where(c => c.FullName.IndexOf(" ") > 0 &&
c.FullName.Substring(c.FullName.LastIndexOf(" ")+1).StartsWith(lastNameSearch));



任何线索如何实现这一目标而无需使用LastIndexOf()?也许我要检查这个后我抢用了ToList从数据库的结果()?

Any clue how to achieve this without using LastIndexOf()? Maybe I have to check for this after I grab results from the database using ToList()?

推荐答案

您是通过设定的限制的href=\"http://msdn.microsoft.com/en-us/library/bb738681.aspx\">规范函数可以被翻译成一个SQL查询,因此任何解决方案必须没有实现超过规范函数提供的。

You are limited by the set of canonical functions that can be translated into an SQL query, so any solution must be achieved with no more than the canonical functions offer.

幸运的是,所支持的功能之一是布尔包含(字符串)实例方法。你可以重写你的支票

Luckily, one of the supported functions is the bool Contains(string) instance method. You can rewrite your check as

persons = persons.Where(c => c.FullName.Contains(" " + lastNameSearch));

这是没有的究竟的喜欢你目前的版本(因为它将使人们有多个名称,以满足他们的第二个名字,而前者不会),但它是相当接近,恕我直言,是可以接受的。

This is not exactly like your current version (because it will allow people with more than one name to match their second name, while the former won't), but it's pretty close and IMHO can be acceptable.

当然,这将是更比任何的这更好地保持过去的名称作为在数据库中的单独的列,如果这是在所有可能的

Of course it would be much better than any of this to keep the last names as a separate column in the database, if that is at all possible.

这篇关于获取与LINQ字符最后索引实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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