Komplexe String-Matching mit LINQ to Entity Framework
-
05-07-2019 - |
Frage
Wir sind mit LINQ to EF, eine Lösung zu entwickeln.
In meiner LINQ-Abfrage würde Ich mag
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;
I „*“ als Platzhalter und eine regex Lösung versucht, ohne Erfolg - Gibt es noch andere Optionen
?Lösung
anstelle von Equals versuchen Sie es mit Enthält sollte Ihre Wildcards nehmen, weil intern LINQ verwendet LIKE , wenn Sie enthält
var results = from a in mycontainer.users
where a.fullname.Contains(newString)
select a.fullname;
Andere Tipps
Es ist ein großer Artikel, der beschreibt, wie es geht: Regex in Entity Framework
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow