0

I'd like to have a database field with wildchars. It's kind of reverse wildchar linq search.

So my field would contain "100-50-*", and I would like to find this, when i search for anything starting with "100-50-". So searches like "100-50-10", "100-50-3", "100-50-B" would all find my row.

Edit: I'm not sure i explained this properly. I don't know the contents of the database before the search. Field values could be "100-" or "100-50-" or "100-50-10". I would like all three records to show up, if i search for 100-50-10.

Thanigainathan's answer was actually pretty close to what i was looking for.

Simon Larsen
  • 63
  • 1
  • 5

3 Answers3

1

You can use something in the lines of

string searchString = "100-50-B";
string regex = "[0-9]{3}-[0-9]{2}";

var match = new Regex(regex).Match(searchString).Value;

var matches = DataContext.Table.Where(e => e.StartsWith(match));

The regex will extract XXX-XX (only this pattern as ints) and search for items starting with that.

Jan Johansen
  • 1,999
  • 6
  • 30
  • 43
0

Here's a quick test I did that seems to solve the problem.

List<string> test = new List<string> { "100-50-1,", "100-50-2", "100-50-3", "100-60-6" };
var items = (from s in test where s.StartsWith("100-50") select s).ToList();

Using StartsWith you can build whatever pattern you're looking for.

MikeHe
  • 68
  • 6
0

Please try this one.

   var dataSource = new List<string>() { "100-50-*" };
   var filteredData = dataSource.Where(l => "100-50-10".StartsWith(l.Replace("*",string.Empty))).ToList();
Thanigainathan
  • 1,505
  • 14
  • 25