Click here to Skip to main content
11,432,168 members (55,150 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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".

============
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 21-Jan-13 22:55pm
Edited 21-Jan-13 23:37pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

foreach (string itemNCEMID in NewContainsExistingMemberInDialog)
                        {
                             if (DialogAdditionDistinct.Any(accEntity => accEntity.Equals(itemNCEMID )))
                            {
                                DialogAdditionDistinct.Remove(n);
 
                            }
                       }
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

There is the Except method in LINQ and in your example should be used in the following way.



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:
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
  Permalink  
v2
Comments
wonder-FOOL at 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 at 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 at 22-Jan-13 4:37am
   
Ok I have updated my question. I hope that clears it out.
wonder-FOOL at 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 at 22-Jan-13 5:00am
   
Based on your edit, check mine. Cheers
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I think this code will work in your case.

DialogAdditionDistinct = DialogAdditionDistinct.Except( NewContainsExistingMemberInDialog).ToList();
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

OK I have found it thanks for the replies all.
foreach (var p in DialogAdditionDistinct.Intersect(NewContainsExistingMemberInDialog).ToList())
                            DialogAdditionDistinct.Remove(p);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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

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


your DialogAdditionDistinct will be as you need
  Permalink  

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



Advertise | Privacy | Mobile
Web04 | 2.8.150428.2 | Last Updated 22 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100