比较前删除字符串中的空格
我想根据电话号码获取联系人,但在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用:
这与您在 linq 表达式中所做的几乎相同。
Use :
It's pretty much the same as what you're doing within the linq expression.
这将在字符串中任何位置的所有空格上进行拆分,并重新加入结果数组而不包含空格。我相信这就是您正在寻找的。
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.
不知道 String.Replace 的效率是否更高或更低,但
会删除所有空白字符。
Don't know if it's more or less efficient that String.Replace, but
will strip out all the whitespace chars.
将
.Contains
替换为.Equals
。我不确定您的提供程序是否支持String.Contains
,但这可能会导致您的错误。尽管如此,将“0123 1234”分类为“231”的匹配并没有多大意义。Replace your
.Contains
with.Equals
. I am not sure if your provider supportsString.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".