我想根据电话号码获取联系人,但在 Ms Dynamics 中,电话号码以各种格式存储,如 123 45 678、12 34 56 78、0112345678、01 12345678 等。
所以我必须在进行比较之前删除它们中的空格,我确实尝试在字符串上使用 Replace 方法,但这在运行时给了我一个非法方法错误。
我真的必须检索所有联系人并进行另一个循环进行比较,还是有办法“清理”查询中的字符串?
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”条件。实体成员正在调用无效的属性或方法。
最佳答案
使用:
phone = phone.Replace(" ", ""); // Replaces whitespace with empty string
这与您在 linq 表达式中所做的几乎相同。
关于c# - 比较前删除字符串中的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5992777/