Corrispondenza di stringhe complesse con LINQ to Entity Framework
-
05-07-2019 - |
Domanda
Stiamo usando LINQ to EF per sviluppare una soluzione.
Nella mia query LINQ vorrei
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;
Ho provato " * " come jolly e soluzione regex, inutilmente: ci sono altre opzioni?
Soluzione
invece di usare Uguali prova a usare Contiene questo dovrebbe prendere i tuoi caratteri jolly perché internamente LINQ usa MI PIACERE quando usi Contiene
var results = from a in mycontainer.users
where a.fullname.Contains(newString)
select a.fullname;
Altri suggerimenti
C'è un ottimo articolo che descrive come farlo: Regex in framework di entità
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow