Comparing Two Lists and Return Not Matching Items Results With Error

Comparing two lists and return not matching items results with error

Use Any:

using System;

using System.Collections.Generic;
using System.Linq;

public class Program
{
public static void Main()
{

var sent = new List<Sent>()
{
new Sent { Address = "04004C", Data = "55AA55" },
new Sent { Address = "040004", Data = "0720" },
new Sent { Address = "040037", Data = "31" },
new Sent { Address = "04004A", Data = "FFFF" }
};


var res = new List<Result> () {
new Result { AddressOK = "04004C", DataOK = "55AA55" },
new Result { AddressOK = "040004", DataOK = "0721" },
new Result { AddressOK = "040038 ", DataOK = "31" },
new Result { AddressOK = "04004A", DataOK = "FFFF" }

};

var diff =
sent.Where (s => !res.Any (r => s.Address == r.AddressOK && s.Data == r.DataOK ));


foreach (var item in diff)
{
Console.WriteLine("{0} {1}", item.Address, item.Data);
}

}
}


public class Sent
{
public string Address;
public string Data;
}


public class Result
{
public string AddressOK;
public string DataOK;
}

Output:

040004 0720
040037 31

Live Code: https://dotnetfiddle.net/ZVuiPd

How can I compare two lists in python and return not matches

Just use a list comprehension:

def returnNotMatches(a, b):
return [[x for x in a if x not in b], [x for x in b if x not in a]]

Compare Items in two Lists and return non matching Items

You have't overridden Equals and GethashCode in your Farmer class. That's why (a != b) doesn't work and also Enumerable.Except fails for the same reason: only references are compared and both are different instances.

How should .NET know that the ContractNumber of the farmer is relevant to identify him? One wayy is to tell it by overriding Equals and GetHashCode:

public class Farmer : IEquatable<Farmer>
{
public string FarmerName { get; set; }
public string ContractNumber { get; set; }

// .... other properties etc

public bool Equals(Farmer other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return string.Equals(ContractNumber, other.ContractNumber);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((Farmer) obj);
}

public override int GetHashCode()
{
return (ContractNumber != null ? ContractNumber.GetHashCode() : 0);
}
}

Now you can use Except:

Farmer = CSVFarmers.Except(Farmers).ToList();

2nd way is to implement a custom IEqualityComparer<Farmer>, f.e if you can't change the Farmer class itself or you don't want to change it's behaviour and just want a custom comparer:

public class FarmerContractComparer : IEqualityComparer<Farmer>
{
public bool Equals(Farmer x, Farmer y)
{
if (ReferenceEquals(x, y)) return true;
if (ReferenceEquals(null, x) || ReferenceEquals(null, y)) return false;
return x.ContractNumber == y.ContractNumber;
}
public int GetHashCode(Farmer obj)
{
return (obj.ContractNumber != null ? obj.ContractNumber.GetHashCode() : 0);
}
}public class FarmerContractComparer : IEqualityComparer<Farmer>
{
public bool Equals(Farmer x, Farmer y)
{
if (ReferenceEquals(x, y)) return true;
if (ReferenceEquals(null, x) || ReferenceEquals(null, y)) return false;
return x.ContractNumber == y.ContractNumber;
}
public int GetHashCode(Farmer obj)
{
return (obj.ContractNumber != null ? obj.ContractNumber.GetHashCode() : 0);
}
}

You can use this comparer in many LINQ methods, for example also in Enumerable.Except:

Farmer = CSVFarmers.Except(Farmers, new FarmerContractComparer()).ToList();

This approach has the advantage that you could provide different comparers for different tasks.


3rd approach: use LINQ and don't create a new class(less reusable and efficient but less work):

Farmer = CSVFarmers.Where(f => !Farmers.Any(f2 => f.ContractNumber == f2.ContractNumber)).ToList();

How to compare two lists of strings and return the matches

Try this (quick and dirty):

animals = ["dog", "bear", "monkey", "bird"]
pets = ["dog", "bird", "cat", "snake"]

print("The original list 1 : " + str(animals))
print("The original list 2 : " + str(pets))
res = []
for a in animals:
for p in pets:
if a == p:
res.append(a)


print("The Match indices list is : " + str(res))

How can I compare two lists in python and return matches

Not the most efficient one, but by far the most obvious way to do it is:

>>> a = [1, 2, 3, 4, 5]
>>> b = [9, 8, 7, 6, 5]
>>> set(a) & set(b)
{5}

if order is significant you can do it with list comprehensions like this:

>>> [i for i, j in zip(a, b) if i == j]
[5]

(only works for equal-sized lists, which order-significance implies).

How to compare two List content data and return mismatch value?

The problem is with this line:

List<string> differ = null;

After that you try to add something to the list, but the list is null. There's your exception.

You could change it as follows:

List<string> differ = new List<string>();

But easier is to replace this line:

List<string> difference = Comparator(list1, list2);

By:

List<string> difference = list1.Except(list2).ToList();

Easiest way of comparing two lists in C# with LINQ and adding missing items to Entity Class

You can use LINQ:

DBList.AddRange(prepared.Where((i) => DBList.FindIndex((el) => el.Transaction.ID == i.TransactionID) == -1));


Related Topics



Leave a reply



Submit