-3

So, I have a list of integers called itemIds and a list of objects called items, how can I rewrite this :

 List<int> itemIds;
 List<object> items;

 for (itemIds.Select(old => (!items.Any(i => i.itemId == old))))
    {
          ItemService.ItemDeleteById(old);
    }

Now it fails, it asks for ";" after the last paranthesis of the for and it doesn't recognize the old variable.I want to delete the items that meet that condition. Thanks !

Liam
  • 27,717
  • 28
  • 128
  • 190
Frey_ja
  • 51
  • 13

4 Answers4

1

You can use foreach. Also use .Where() instead of .Select():

foreach (var itemId in itemIds.Where(old => !items.Any(i => i.itemId == old)))
{
    ItemService.ItemDeleteById(itemId);
}
Peter B
  • 22,460
  • 5
  • 32
  • 69
1

If you hashed itemIds, you'll probably have a more performant loop:

HashSet<int> itemIds;
List<object> items;

foreach (var item in items.Where(i => itemIds.Contains(i.itemId))
{
   ItemService.ItemDeleteById(item.itemId);
}
Parrish Husband
  • 3,148
  • 18
  • 40
1

You can write it as:

foreach(var x in itemsIds.Where(old => items.Contains(old)){
        ItemService.ItemDeleteById(x);
    }

EDIT: Outspeedded by Parrish Husband

Kerlos Bekhit
  • 128
  • 1
  • 7
0

try foreach instead of for and Select doesn't receive a predicate true/ false, use Where instead

foreach (var item in itemIds.Where(old => !items.Any(i => i.itemId == old)))
    {
          ItemService.ItemDeleteById(item);
    }
Antoine V
  • 6,998
  • 2
  • 11
  • 34