I’m having a little trouble with using regex in linq. I want to be able to perform a search on values in my database. The goal is to insert a value into the regex and then use that to search a column in a table in my database for that term. I want it to work so that it gets all exact matches and also returns matches in which the search term is a substring of the term in the column.
I was trying to follow this tutorial on msdn, but it doesn’t quite fit perfectly with my problem:
public List<Event> GetEventsByName(string eventString)
{
PollDbDataContext db = new PollDbDataContext();
Regex eventName = new Regex(eventString, RegexOptions.IgnoreCase);
var matchingEvents =
from events in db.Events
let matches = eventName.Matches(events.name)
where eventName.Matches(events.name).Count > 0
orderby events.name.Count() descending
select events;
return matchingEvents.ToList();
}
I’d appreciate any advice.