0

I have 2 lists ListOfItemsToControl and lstRemoveItems. I want to remove ListOfItemsToControl where lstRemoveItems.sItemName matches ListOfItemsToControl.sItemName

How can this be done ?

Mark Hall
  • 53,938
  • 9
  • 94
  • 111
user1438082
  • 2,740
  • 10
  • 48
  • 82
  • i can add 2 lists using concatenation and i know that i can loop through the list but there must be a single line of LINQ code to do the job – user1438082 Feb 01 '14 at 21:04
  • 1
    As usual, Jon Skeet has answered this. http://stackoverflow.com/questions/853526/using-linq-to-remove-objects-within-a-listt – Richard Schneider Feb 01 '14 at 21:05

2 Answers2

1
var names = lstRemoveItems.Select(y => y.sItemName).ToList();
var result = ListOfItemsToControl.Where(x => names.Contains(x.sItemName)).ToList();
var resultList = ListOfItemsToControl.Except(result);

That seems odd but it should work :)

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
1

Here's a LINQ one-liner. I haven't tested this, but it should work:

ListOfItemsToControl = ListOfItemsToControl.Where(l => !lstRemoveItems.Any(r => r.sItemName == l.sItemName)).ToList();
brcpar
  • 96
  • 3