Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq on List<object>

Existing legacy code is as follows:

List<object> myItems;
//myItems gets populated by a method call

foreach (object[] item in myItems)
{
  string Id = item[0].ToString();
  string Number = item[1].ToString();

  //now do some processing if Number satisfies some criteria
}

would like to convert this using linq to select all Ids that match a certain Number.

All suggestions would be appreciated.

Thanks.

like image 281
N. Kaufman Avatar asked Aug 19 '26 10:08

N. Kaufman


1 Answers

Use Select() and Where()

bool IsSatisfyingNumber(String number) {
    // True if number satisfies some criteria
}

List<String> matchingIds = myItems
    .Where(item => IsSatisfyingNumber(item[1].ToString()))
    .Select(item => item[0].ToString())
    .ToList();
like image 194
Bagdan Gilevich Avatar answered Aug 20 '26 23:08

Bagdan Gilevich



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!