Click here to Skip to main content
15,886,693 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two sets of lists and I wish to get the difference between the two lists but for my logic I do not seem to be getting expected results :

LIST A
A002
A75
B908
123456
672314
756213

LIST B
htg1
EDDIE1
EDDIE2
A002
A75
B908

Expected Results
To get all the new codes in List B that are not already maintained in the mapping list (List A)
This should give me all new items as below :
htg1
EDDIE1
EDDIE2


Current Output
When I apply LINQ logic for filtering I am getting all the items in List B :
htg1
EDDIE1
EDDIE2
A002
A75
B908


This is because this join query is returning 0 rows :
 List<string> joinItems = new List<string>();

joinItems = (from d1 in mappings
                         join d2 in references on d1.MappingId equals d2.CustCode
                         select d1.MappingId).ToList<string>();


Where mappings represents resultset for LIST A :

C#
List<Partner> mappings = GetMappingsAsModel();


and references represents resultset for LIST B :

C#
List<CustomerCode> references = GetCustomerCodes();


And to find the differences I am doing this :

C#
List<string> cuscodes = references.Select(x => x.CustCode.ToString()).ToList();
          
           
 var newItems = cuscodes.Except(joinItems);

            
int newCodes = cuscodes.Except(joinItems).Count();



What is wrong with my Join query above ?

What I have tried:

I have tried the following alternative but still get nothing since the join query is returning nothing :

var result = references.Where(p => !mappings.All(p2 => p2.MappingId == p.CustCode)).ToString().ToList();
Posted
Updated 1-Jun-21 0:41am

If your join query is returning zero rows, then the MappingIds do not match the CustCode values.
If I try your code with pure strings:
C#
List<string> mappings = new List<string>() { "A002", "A75", "B908", "123456", "672314", "756213" };
List<string> references = new List<string>() { "htg1", "EDDIE1", "EDDIE2", "A002", "A75", "B908" };
List<string> joinItems = new List<string>();

joinItems = (from d1 in mappings
             join d2 in references on d1 equals d2
             select d1).ToList<string>();

Then I get what I expect: three strings "A002", "75", and "B908"

So ... start with the debugger, and look at exactly what your Lists contain, and what relation there is between the Ids and CustCodes - I'd suspect you want CustID instead of CustCode, but I have no access to your data, so I can't be sure.
 
Share this answer
 
Comments
BillWoodruff 1-Jun-21 13:12pm    
+5
three strings "A002", "75", and "B908"

those are the intersection of the two sets; the OP is after what's in B and not in A: htg1 EDDIE1 EDDIE2
Ralf Meier 1-Jun-21 14:17pm    
+5very interesting Solution.
I never thought that it could also be realized in this way - until today I've allready realized that with "conventionell programming".
Thank for it ... :-)
OriginalGriff 1-Jun-21 16:43pm    
You're welcome!
Quote:
Expected Results
To get all the new codes in List B that are not already maintained in the mapping list (List A) This should give me all new items as below :

htg1
EDDIE1
EDDIE2
This "translates" as:

List<string> diffBA = ListB.Except(ListA).ToList();

Get your two Lists in the right shape, and it's this simple.
 
Share this answer
 
v2
Comments
Richard Deeming 1-Jun-21 10:32am    
You'd need a .ToList() on the end of that - .Except(...) returns an IEnumerable<T>. :)
BillWoodruff 1-Jun-21 11:27am    
Thanks !

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