0

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
EDD1E2
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

Actual 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 :

List<Partner> mappings = GetMappingsAsModel();

and references represents resultset for LIST B :

List<CustomerCode> references = GetCustomerCodes();

And to find the differences I am doing this :

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 ?

Golide
  • 835
  • 3
  • 13
  • 36
  • When you say differences, do you just want all the items in both lists that are not in the other list? If so it's basically the opposite of intersection - there's an answer on that here: https://stackoverflow.com/questions/5620266/the-opposite-of-intersect (note, it's the second example on the accepted answer) – Charleh Jun 01 '21 at 09:28
  • 2
    Does this answer your question? [Get the difference between two lists using LINQ](https://stackoverflow.com/questions/7347386/get-the-difference-between-two-lists-using-linq) and [Difference between two lists](https://stackoverflow.com/questions/5636438/difference-between-two-lists) and [Get the differences between 2 lists](https://stackoverflow.com/questions/10810438/get-the-differences-between-2-lists) –  Jun 01 '21 at 09:31

3 Answers3

1
    var list1 = new List<string> {
    "A002",
    "A75"  ,
    "B908"  ,
    "123456",
    "672314",
    "756213"};
    
    var list2 = new List<string> {
    "htg1"  ,
    "EDDIE1",
    "EDD1E2",
    "A002"  ,
    "A75"   ,
    "B908"    
    };
    
    
    
    foreach(var item in list2.Except(list1))
    {
    Console.WriteLine(item);
    }
Ramji
  • 375
  • 2
  • 14
0

You can do it using Except LINQ method:

        static void Main(string[] args)
    {
        var list1 = new List<string> {
            "A002",
            "A75"  ,
            "B908"  ,
            "123456",
            "672314",
            "756213"};

        var list2 = new List<string> {
            "htg1"  ,
            "EDDIE1",
            "EDD1E2",
            "A002"  ,
            "A75"   ,
            "B908"
        };

        var difference = list2.Except(list1);

       Console.WriteLine(string.Join(", ", difference));

Result: htg1, EDDIE1, EDD1E2

This is a working example. Make sure your lists of codes are actually filled in with the values that you've provided as an example.

Dragos Stoica
  • 1,753
  • 1
  • 21
  • 42
0

Using the Except method which produces the set difference of two sequences (in your case, a list of strings) is a nice and clean approach.

List<string> A = new List<string> {"A002", "A75" ,"B908" ,"123456", "672314", "756213"};
List<string> B = new List<string> {"htg1" ,"EDDIE1", "EDD1E2", "A002","A75" ,"B908"};

List<string> diffs = A.Except(B).ToList(); // ["123456", "672314", "756213"];

foreach(var diff in diffs)
{
    // work with each individual string out of diffs here
}

You can read more about Except here:

https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.except?view=net-5.0

Ran Turner
  • 14,906
  • 5
  • 47
  • 53