This is similar to my last question; but from a different angle.
See if item exists once in Enumerable (Linq)
Given the following set of items, and lists containing them…
Item 1
Item 2
Item 3
Item 4
Item 5
class Item
{
string Name { get; set; }
}
List<Item> available = new List<Item>()
{
Item 1
Item 1
Item 2
Item 3
Item 5
}
List<Item> selected = new List<Item>()
{
Item 1
Item 2
Item 3
}
I need to make a third List that has everything from “available”, except what is in “selected”.
However ‘Item 1’ is in ‘available’ twice, but only in ‘selected’ once. Since they are instances of the same item, I am having trouble figuring out the appropriate logic to accomodate this.
The final array should look like…
List<Item> selectable = new List<Item>()
{
Item 1
Item5
}
There may be a LINQ method to accomplish this task. You could certainly get the 5 item since it is unique, but the second item 1 could prove difficult. However, you could always do this the old fashioned way and create the new list yourself. Consider this example:
…
You create a list for the items still available, which is initially empty. You create a list for the items still selected, which contains all of the selected items. You then simply iterate over the available items and search the stillSelected list. If the item is found, you remove it from the stillSelected list. If not, you add it to the stillAvailable list. By the end of the loop, stillAvailable will contain a single item 1 and item 5.