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

Qwa*_*ark 2 c# linq linq-to-entities dynamics-crm dynamics-crm-online

我希望根据电话号码获取联系人,但在动态Ms中,电话号码以各种格式存储,如123 45 678,12 34 56 78,0112345678,01 12345678等.

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

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

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...    
   };
Run Code Online (Sandbox Code Playgroud)

编辑:

这是异常消息:

"where"条件无效.实体成员正在调用无效的属性或方法.

Llo*_*ell 7

使用 :

phone = phone.Replace(" ", ""); // Replaces whitespace with empty string
Run Code Online (Sandbox Code Playgroud)

它与你在linq表达式中所做的几乎相同.

  • 尝试:哪里(c.Telephone1.Replace("","")== phone)|| (c.MobilePhone.Replace("","")==电话) (2认同)