Entity Framework: How to express a LIKE statement that contains [0-9]
I would like to construct a LINQ query for use against an EF context that selected rows based on a LIKE condition.
As I understand you can get this behaviour using .Contains()
. However, the LIKE I would like to include contains a number range:
LIKE 'LN[0-9]%'
for example...
How would I go about replicating this beh开发者_开发问答aviour in a simple query?
Thanks,
Currently u can't use like in LINQ directly. U can use StartsWith
, Contains
or EndsWith
that are translated to LIKE. Other solution is using 'Where' overide with 'string' like this:
Where("something LIKE 'LN[0-9]%'")
Here is example
try StartsWith like so:
var query = Cities.Where(city => city.Code.StartsWith("LN[0-9]"));
This is a regex problem ([0-9] is a regex nomenclature). I suggest you try something like this:
Regex myRegex = new Regex("LN[0-9]");
var matches = Cities.Where(c => myRegex.IsMatch(c.Field));
A crude example but should get you started.
精彩评论