Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

I had the following code which works fine but I am looking for a finer solution.
====EDIT====
My Items in the lists are ;
NewContainsExistingMemberInDialog: "B", "C", "D", "E"
DialogAdditionDistinct: "A", "B", "C"

Final output:
IntersectedAndRemovedList which is DialogAdditionDistinct. It should only contain "A".

============
C#
List<int> ItemsToBeDeletedAtIndex = new List<int>();
                        int index = -1;

                        foreach (string itemCAD in DialogAdditionDistinct)
                        {
                            index++;
                            foreach (string itemNCEMID in NewContainsExistingMemberInDialog)
                            {
                                if (itemNCEMID == itemCAD)
                                {
                                    ItemsToBeDeletedAtIndex.Add(index);
                                }
                            }
                        }

                        int indexCounter = 0;
                        foreach (int itemIndex in ItemsToBeDeletedAtIndex)
                        {
                            DialogAdditionDistinct.RemoveAt(itemIndex - indexCounter);
                            indexCounter++;
                        }


What this code does finds the same items between the two list and saves the index of the item. After that removing the items in the matching index.

So basically is there a LINQ code or much optimized code version of this finding the intersection between two list and removing the intersected items from one of the list.

Thank you very much for your time and understanding.
Posted
Updated 21-Jan-13 22:37pm
v2

C#
foreach (string itemNCEMID in NewContainsExistingMemberInDialog)
                        {
                             if (DialogAdditionDistinct.Any(accEntity => accEntity.Equals(itemNCEMID )))
                            {
                                DialogAdditionDistinct.Remove(n);

                            }
                       }
 
Share this answer
 
v2
There is the Except method in LINQ and in your example should be used in the following way.



C#
List<string> a = new List<string>() { "A", "B", "C", "D" };
List<string> b = new List<string>() { "A", "B", "E", "F" };

List<string> c = a.Except(b).ToList<string>();


What you will find in the list c are two elements, C and D, the once that are contained in the list a and not in the list b.

By searching on google you will find more examples.

Cheers

EDIT:
Based on new information this is a possible solution:
C#
List<string> a = new List<string>() { "B", "C", "D", "E" };
List<string> b = new List<string>() { "A", "B", "C" };

a = b.Except(a).ToList<string>();


Cheers
 
Share this answer
 
v2
Comments
wonder-FOOL 22-Jan-13 4:20am    
Thank you for your answer but I have already knew the Except method but I couldnt find a way to remove the items between the list that I want the items to be removed and the items that is intersected.
Mario Majčica 22-Jan-13 4:22am    
Can you make an example of the items in collection A and items in collection B together with the result you are expecting? It is not clear by your code. Then we can study a correct LINQ statement that will do. Cheers
wonder-FOOL 22-Jan-13 4:37am    
Ok I have updated my question. I hope that clears it out.
wonder-FOOL 22-Jan-13 4:44am    
Thanks but I have found it with a much simplier and optimized way. I have provided it as a solution.
Mario Majčica 22-Jan-13 5:00am    
Based on your edit, check mine. Cheers
I think this code will work in your case.

DialogAdditionDistinct = DialogAdditionDistinct.Except( NewContainsExistingMemberInDialog).ToList();
 
Share this answer
 
v3
OK I have found it thanks for the replies all.
C#
foreach (var p in DialogAdditionDistinct.Intersect(NewContainsExistingMemberInDialog).ToList())
                            DialogAdditionDistinct.Remove(p);
 
Share this answer
 
well i don't know why you need to remove the items instead of creating a new list without it...

in any case that should work

SQL
DialogAdditionDistinct.Where(i => NewContainsExistingMemberInDialog.Contains(i)).ToList().ForEach(j => DialogAdditionDistinct.Remove(j));



your DialogAdditionDistinct will be as you need
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900