Equality Comparison with Multiple Instances/IEqualityComparer problems in LINQ?

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: class Item { public Item(string name) { Name = name; } public string Name { get; set; } } List available = new List() { new Item("1"), new Item("1"), new Item("2"), new Item("3"), new Item("5") }; List selected = new List() { new Item("1"),new Item("2"), new Item("3") }; List stillAvailable = new List(); List stillSelected = new List(selected); foreach (Item item in available) { Item temp = stillSelected. Find(i => i. Name == item.Name); if (temp == null) stillAvailable.

Add(item); else stillSelected. Remove(temp); } 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.

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: class Item { public Item(string name) { Name = name; } public string Name { get; set; } } ... List available = new List() { new Item("1"), new Item("1"), new Item("2"), new Item("3"), new Item("5") }; List selected = new List() { new Item("1"),new Item("2"), new Item("3") }; List stillAvailable = new List(); List stillSelected = new List(selected); foreach (Item item in available) { Item temp = stillSelected. Find(i => i. Name == item.Name); if (temp == null) stillAvailable.

Add(item); else stillSelected. Remove(temp); } 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.

This is a kinda tricky approach, but it gets the job done. I borrowed from the "Decorate-Sort-Undecorate" idiom in Python, which does sorting by associating a temporary sort key with an array, combined with the fun and useful fact that anonymous types in . NET have a default EqualityComparer that compares based on the values of their fields.

Step 1: Group the elements in each list by Name, and then associate an index with each item within each group, and flatten the groups back into a regular list: var indexedAvailable = available. GroupBy(i => i. Name) .

SelectMany(g => g. Select((itm, idx) => new { Name = itm. Name, Index = idx })); var indexedSelected = selected.

GroupBy(i => i. Name) . SelectMany(g => g.

Select((itm, idx) => new { Name = itm. Name, Index = idx })); This will turn the lists into these: indexedAvailable indexedSelected Name = Item 1, Index = 0 Name = Item 1, Index = 0 Name = Item 1, Index = 1 Name = Item 2, Index = 0 Name = Item 2, Index = 0 Name = Item 3, Index = 0 Name = Item 3, Index = 0 Name = Item 5, Index = 0 Now you can see that in the indexed lists, each numbered occurrence of any Name in available will match only with the equivalent-numbered occurrence of the same name in selected. So you can use a simple Except to remove anything in indexedAvailable that isn't in indexedSelected, and then "undecorate' by turning the anonymous-typed indexed objects back into Items.

Var selectable = indexedAvailable. Except(indexedSelected) . Select(i => new Item() { Name = i.Name }); And the proof: foreach (var item in selectable) Console.

WriteLine(item. Name); //prints out: //Item 1 //Item 5 Note that this will work even if selected contains names that aren't in available, e.g. If the second list has an Item 4 like in your last question.

Var comp = new MyEqualityComparer(); selectable = available. Distinct(comp). Except(selected, comp).

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions