Quote:
Essentially I need to loop through the Reference fields in ListView1 - find the corresponding\matching entry in Listview2 and compare the State and Description fields across the 2 ListView controls..
You're wrong! You have to create a list of custom class and implement
Equals
method. How? See:
How to define value equality for a class or struct - C# Programming Guide | Microsoft Docs[
^]
Here's an idea:
void Main()
{
List<MyLvObject> l1 = new List<MyLvObject>()
{
new MyLvObject(){Reference = 1, State="aaa", Description="Bbb"},
new MyLvObject(){Reference = 2, State="ccc", Description="ddd"},
new MyLvObject(){Reference = 3, State="eee", Description="fff"},
};
List<MyLvObject> l2 = new List<MyLvObject>()
{
new MyLvObject(){Reference = 2, State="ccc", Description="ddd"},
new MyLvObject(){Reference = 8, State="xxx", Description="z"},
};
foreach(MyLvObject m1 in l1)
{
Console.WriteLine(m1.ToString());
foreach(MyLvObject m2 in l2)
{
Console.WriteLine($"\t{m2.ToString()} => {m1.Equals(m2)}");
}
}
}
public class MyLvObject: IEquatable<MyLvObject>
{
public int Reference {get; set;} = 0;
public string State {get; set;} = string.Empty;
public string Description {get; set;} = string.Empty;
public override int GetHashCode() => (State, Description).GetHashCode();
public bool Equals(MyLvObject mlo)
{
if (mlo is null || !this.GetType().Equals(mlo.GetType()))
{
return false;
}
else
{
return State.Equals(mlo.State) && Description.Equals(mlo.Description);
}
}
public override string ToString()
{
return $"{Reference} {State} {Description}";
}
}
Result:
1 aaa Bbb
2 ccc ddd => False
8 xxx z => False
2 ccc ddd
2 ccc ddd => True
8 xxx z => False
3 eee fff
2 ccc ddd => False
8 xxx z => False
Note: you can use overloaded method of
String.Equals
with
InvariantCultureIgnoreCase
parameter to ignore upper/lower case. See:
String.Equals Method (System) | Microsoft Docs[
^]