Assume a DbSet<Thing> things
; and Thing
has a Name
property.
If I want to retrieve a list of things that match a list of names, it's pretty straightforward:
var names = new List<string>{"John", "Jacob", "Jinkelheimer", "Smith"};
return things.Where(t => names.Contains(t.name));
However, what if I want to match a list of partial name possibilities?
var nameBits = (new List<string>{"ohn", "mit"}).ToLower();
// this doesn't seem good / right / efficient
things.Where(t => nameBits
.Select(bit => EF.Functions.Like(t.Name, $"%{bit}%")
.Any(m => m)
);
// I could also go with:
things.Where(t => nameBits
.Select(bit => t.Name.Contains(bit)).Any(m => m)
);
... but I don't know how efficient any of those will be.
How would I write the linq query that would efficiently match "John", "Johnathan", "Smith", "Smitty", etc using ohn
and mit
?