2017-05-26 33 views
0

我有下面的代碼,它會產生一個錯誤,它不支持正則表達式。我相信Async正在將它轉換爲SQL聲明,這是使它失敗?如何在異步實體框架中使用正則表達式

Regex emailRegEx = new Regex(@"^.*" + emailDomain + "$"); 

var peoplesWithEmail = await personService.Query().Where(p => p.Emails.Any(e => emailRegEx.Match(e.EmailAddress).Success == true)).ToListAsync(); 

錯誤消息

「ExceptionMessage」:「LINQ實體無法識別方法 'System.Text.RegularExpressions.Match匹配(System.String)' 方法,和這種方法不能被翻譯成商店表達。「

那麼這是什麼工作?

回答

-1

,我認爲你應該使用Regex.IsMatch

 var peoplesWithEmail = await personService.Query().Where(p => p.Emails.Any(e => emailRegEx.IsMatch(e.EmailAddress))).ToListAsync(); 
1

是因爲實體框架是翻譯的LINQ to SQL,它是失敗的。

正則表達式是不支持LINQ到SQL,但是,你可以使用String.StartsWithString.EndsWithString.Contains

var peoplesWithEmail = await personService.Query() 
     .Where(p => p.Emails.Any(e => e.EndsWith(emailDomain)).ToListAsync(); 
相關問題