Search in a List<DataRow>?

11,747

Solution 1

var searchValue = SOME_VALUE;
var result = list.Where(row => row["MyColumn"].Equals(searchValue)); // returns collection of DataRows containing needed value
var resultBool = list.Any(row => row["MyColumn"].Equals(searchValue)); // checks, if any DataRows containing needed value exists

Solution 2

If you should make this search often, I think it's not convenient to write LINQ-expression each time. I'd write extension-method like this:

private static bool ContainsValue(this List<DataRow> list, object value)
{
    return list.Any(dataRow => dataRow["MyColumn"].Equals(value));
}

And after that make search:

if (list.ContainsValue("Value"))
Share:
11,747
grady
Author by

grady

Updated on July 02, 2022

Comments

  • grady
    grady almost 2 years

    I have a List which I create from a DataTabe which only has one column in it. Lets say the column is called MyColumn. Each element in the list is an object array containing my columns, in this case, only one (MyColumn). Whats the most elegant way to check if that object array contains a certain value?

  • grady
    grady almost 14 years
    The thing is, the list contains an array which I need to search in...so I think I dont have to search the list, but the array(s) inside the list.