简体   繁体   中英

Search value in List collection

I need to search a name which is stored in collection.

Search criteria: eg: 'Search Name' . If i give 'N' this name should be displayed. If i give any alphabet then all the names which contains the given alphabet should be displayed..the name can contains more than one word.

I am using List collection.

search criteria: eg. 1) a 2) xyz 3) full name 4) Name should display if it contains the given alphabet at any position.

I have .Net 3.5

You can use linq as so

    List<MyObject> results = searchList.Where(x => x.SearchName != null 
         && x.SearchName.Contains(searchString)).ToList();

Basically, you can combine Linq and Regex:

List<string> myList;
List<string> search(string pattern)
{
   Regex regPattern = new Regex(pattern);
   return myList.Where(s => regPattern.IsMatch(s)).ToList();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM