簡體   English   中英

使用LINQ to Entity Framework進行復雜的字符串匹配

[英]Complex string matching with LINQ to Entity Framework

我們正在使用LINQ to EF開發解決方案。

在我的LINQ查詢中,我想

string toMatch = "Matt Cool";

newString = toMatch.Replace(" ", "% ") + "%"; 

// So here newString is 'Matt% Cool%'

/*Now in my datasource, there is no 'Matt Cool', there's 'Matthew Coolguy'.
I would like this query to return that result. 
I would expect this behavior with the 
wildcard. It doesn't work*/

var results = from a in mycontainer.users
              where a.fullname.equals(newString)
              select a.fullname;

我嘗試使用“ *”作為通配符和正則表達式解決方案,但無濟於事-還有其他選擇嗎?

而不是使用Equals嘗試使用Contains,這應該會占用通配符,因為當您使用Contains時,內部LINQ使用LIKE

var results = from a in mycontainer.users
              where a.fullname.Contains(newString)
              select a.fullname;

有一篇很棒的文章描述了如何做到這一點: 實體框架中的正表達式

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM