0

I was wondering if it possible to check if a List contains part of a value. If it finds the value then return the value.

E.g. If the List had values 12345, 14567 and 14785, I want to search if the List contains '123', Is this possible?

If it is can all values that contain '123' be returned?

This is how I add values to the List:

recordFailedPO.Add(Convert.ToInt32(dataGWHeight.Rows[0].Cells[0].Value));

This is how I'm checking for a part of a value:

            if (recordFailedPO.Contains(currentPO))
            {
               // Code Here
            }

Where currentPO is the user input.

Thanks for any help

ghostwar 88
  • 547
  • 1
  • 6
  • 17
  • Not by handling them as integers. Store them as, or convert them to strings and use `.Where(s => s.Contains(currenyPO))`. See [duplicate](http://stackoverflow.com/questions/10488587/find-substring-in-a-list-of-strings). – CodeCaster Dec 18 '16 at 20:25

2 Answers2

2

In that case you could use string Contains method

var containsNumber = recordFailedPO.Where(x => x.ToString().Contains("123"));
Maksim Simkin
  • 9,561
  • 4
  • 36
  • 49
1

try to convert to string before

int res = recordFailedPO.Find(x=>x.ToString().Contains(currentPO));

All values can be returned by FindAll .

 List<int> res = recordPO.FindAll(x=>x.ToString().Contains(currentPO)):
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76