比较前删除字符串中的空格

发布于 2024-11-06 15:43:54 字数 777 浏览 0 评论 0原文

我想根据电话号码获取联系人,但在 Ms Dynamics 中,电话号码以各种格式存储,例如 123 45 678、12 34 56 78、0112345678、01 12345678 等。

因此,在进行比较之前,我必须删除其中的空格,我确实尝试在字符串上使用 Replace 方法,但这在运行时给了我一个 Illegal method 错误。

我是否真的必须检索所有联系人并进行另一个循环进行比较,或者有没有办法“清理”查询中的字符串?

string phone = "12345678";
var contacts = from c in orgContext.CreateQuery<Contact>()
    join a in orgContext.CreateQuery<Account>() on c.AccountId.Id equals a.AccountId
    where (c.Telephone1.Replace(" ", "").Contains(phone) || c.MobilePhone.Replace(" ","").Contains(phone))
   select new DynamicContact
   {
     ContactId = c.ContactId,
     FirstName = c.FirstName,
     LastName = c.LastName,
     ....and more...    
   };

编辑:

这是异常消息:

“where”条件无效。实体成员正在调用无效的属性或方法。

I want to get Contacts based on phone number, but in Ms Dynamics phone numbers are stored in all kinds of format like 123 45 678, 12 34 56 78, 0112345678, 01 12345678 and so on.

So I have to remove spaces in them before I do the comparison, I did try to use the Replace method on the string but that gave me an Illegal method error in runtime.

Do I really have to retrieve all the contacts and do another loop for the comparison, Or is there a way to "Clean" the string in the query?

string phone = "12345678";
var contacts = from c in orgContext.CreateQuery<Contact>()
    join a in orgContext.CreateQuery<Account>() on c.AccountId.Id equals a.AccountId
    where (c.Telephone1.Replace(" ", "").Contains(phone) || c.MobilePhone.Replace(" ","").Contains(phone))
   select new DynamicContact
   {
     ContactId = c.ContactId,
     FirstName = c.FirstName,
     LastName = c.LastName,
     ....and more...    
   };

Edit:

This is the Exception message :

Invalid 'where' condition. An entity member is invoking an invalid property or method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

能否归途做我良人 2024-11-13 15:43:54

使用:

phone = phone.Replace(" ", ""); // Replaces whitespace with empty string

这与您在 linq 表达式中所做的几乎相同。

Use :

phone = phone.Replace(" ", ""); // Replaces whitespace with empty string

It's pretty much the same as what you're doing within the linq expression.

你列表最软的妹 2024-11-13 15:43:54
String.Join("", yourString.Split(" "))

这将在字符串中任何位置的所有空格上进行拆分,并重新加入结果数组而不包含空格。我相信这就是您正在寻找的。

String.Join("", yourString.Split(" "))

This will split on all spaces anywhere in the string and rejoin the resulting array without spaces. I believe this is what you are looking for.

我不是你的备胎 2024-11-13 15:43:54

不知道 String.Replace 的效率是否更高或更低,但

new System.String(oldString.Where(x => !Char.IsWhiteSpace(x))); 

会删除所有空白字符。

Don't know if it's more or less efficient that String.Replace, but

new System.String(oldString.Where(x => !Char.IsWhiteSpace(x))); 

will strip out all the whitespace chars.

公布 2024-11-13 15:43:54

.Contains 替换为 .Equals。我不确定您的提供程序是否支持 String.Contains,但这可能会导致您的错误。尽管如此,将“0123 1234”分类为“231”的匹配并没有多大意义。

Replace your .Contains with .Equals. I am not sure if your provider supports String.Contains, but that could possibly cause your error. Despite the fact, that it does not make a lot sense to classify "0123 1234" as a match for "231".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文