locked
Using wildcard to filter a list. RRS feed

  • Question

  • User269416624 posted

    I have a list of objects that I want to filter based upon a string that may contain wildcards.  So for instance I may have something like this:

    theList = context.Items.Where(I => i.itemName == searchString);

    But I want to modify this so that I could have a searchString like "*abc*" and it would filter the items so that theList contained any items where the itemName contains "abc".  I'm not seeing an extension method that looks like it would do this, but maybe I'm missing something.

    Any ideas?

    Saturday, July 4, 2015 4:02 PM

Answers

  • User2103319870 posted

    I want to modify this so that I could have a searchString like "*abc*

    " and it would filter the items so that theList contained any items where the itemName contains "abc".  I'm not seeing an extension method that looks like it would do this, but maybe I'm missing something.

    Try using Contains method instead of Equal to do a wildcard search

              // using Contains method instead of Equal to do a wildcard search
                theList = context.Items.Where(I => i.itemName.Contains(searchString));
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 4, 2015 7:34 PM

All replies

  • User2103319870 posted

    I want to modify this so that I could have a searchString like "*abc*

    " and it would filter the items so that theList contained any items where the itemName contains "abc".  I'm not seeing an extension method that looks like it would do this, but maybe I'm missing something.

    Try using Contains method instead of Equal to do a wildcard search

              // using Contains method instead of Equal to do a wildcard search
                theList = context.Items.Where(I => i.itemName.Contains(searchString));
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 4, 2015 7:34 PM
  • User269416624 posted

    I saw the contains method, and figured that would work as long as the user didn't put in any wildcards.  We may decide to go with that.  Some users will expect to be able to use wildcards, but others won't.  I guess ultimately the customer will decide.  :)

    Thanks!

    Saturday, July 4, 2015 8:04 PM