4

My question uses the following code -

string searchString = dropdownlist.SelectedValue;

List objects = (from o in Object.List()
                       where dropdownlist.SelectedIndex <= 0 || 
                           o.CustomField.Contains(searchString)
                       orderby o.Date descending
                       select order).ToList();

The situation I am running into is as follows - The items in the drop down list are generic messages such as "Customer '{0}' has '{1}' orders". On the object side, o.CustomField would look something like "Customer 'Bob' has '5' orders". Obviously, this means that

o.CustomField.Contains(searchString)

will not return anything. I've been looking into wildcards but have not been able to achieve a successful return. This is what I was trying for a wildcard set up:

searchString = dropdownlist.SelectedValue.Replace("{0}", "*").Replace("{1}", "*");

I'm wondering if there is any cleaner way than to sit around and parse out all the different substrings between '{' or '}'.

Rufus L
  • 36,127
  • 5
  • 30
  • 43
sebe
  • 139
  • 1
  • 10

3 Answers3

0

You could achieve your goal by using regular expressions. See Regex.Match Method. You can transform your selected string from the dropdown list into a regular expression then use the Match method in your LINQ query.

Regex r = new Regex(String.Format ("Customer {0} has {1} orders", "([^\s]+)",  "([^\s]+)"));

This code:

o.CustomField.Contains(searchString)

can then be changed to:

r.Match(o.CustomField).Success
Tarik
  • 10,810
  • 2
  • 26
  • 40
  • This doesn't work on LINQ – Chesare Nov 08 '21 at 22:50
  • @Chesare Provide some example code by opening a new question. Come back here and point me to the new question. – Tarik Nov 09 '21 at 01:47
  • This code causes **error "LINQ to Entities no reconoce el método 'System.Text.RegularExpressions.Match Match(System.String)'"**: `Regex r = new Regex("HR\\d{4}"); var q = from eq in db.CataEquiposRenta where r.Match(eq.NUMEROINTERNO).Success select new { eq.IDEQUIPO, Number = eq.NUMEROINTERNO.Substring(2) };` – Chesare Nov 13 '21 at 01:27
  • This code (**using `SqlFunctions.PatIndex`**) works fine: `// Formato de numero interno: HR1234 var query = from eq in db.CataEquiposRenta where SqlFunctions.PatIndex("HR[0-9][0-9][0-9][0-9]%", eq.NUMEROINTERNO) == 1 orderby eq.NUMEROINTERNO descending select new { eq.IDEQUIPO, Number = eq.NUMEROINTERNO.TrimEnd().Substring(2) };` – Chesare Nov 13 '21 at 01:28
0

Just do a Regex.Replace on {\d+}, replacing it with *.

Something like:

string s = Regex.Replace(input, @"{\d+}", "*");
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Thanks, I've been doing a bit more investigation into Regex since it was mentioned in the above comment and I think this will work best for what I'm hoping to do. Thanks! – sebe Feb 13 '15 at 20:21
0

Try the approach mentioned here Or use combination of field.StartsWith("Customers") && field.EndsWith("orders") && field.Contains("has")

Community
  • 1
  • 1
ziddarth
  • 1,796
  • 1
  • 14
  • 14