How to compare two List<MyObject1>
with List<MyObject2>
?
So if one the Value has different value it should be possible to check it.
(I know we can use foreach
... But I'd like LINQ solution/)
Thank you!
public sealed class MyObject1
{
public string Name { get; set; }
public string Value { set; get; }
public Guid ID { get; set; }
}
public sealed class MyObject2
{
public string Name { get; set; }
public string Value { set; get; }
public Guid ID { get; set; }
}
The accepted answer of this SO question suggests using Enumerable.SequenceEqual
, documented here.
The method takes two IEnumerable
s and an IEqualityComparer
. It iterates over both your enumerables in parallel and checks equality element by element using the comparer you provide.
In the IEqualityComparer implementation, you may want to compare MyObject instances by their Id properties.
If there really are two object types, you could do something like MyList1.Select(new MyObject2 {/*mapping goes here*/})
or try using AutoMapper
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With