C# compare 2 Lists of <T> -
i want compare 2 lists , edit them while @ it.
each list contains objects hold: version, name, date , state. want compare version number when name equal. want set state different when version not equal.
anyone understands want do? can show me way so?
edit: tried far, wont work.
// first data list<foundassembly> sourcenotcompared = findassemblies(textbox.text); list<foundassembly> targetnotcompared = findassemblies(textbox1.text); // compare list , adjust state property int list1max = sourcenotcompared.count; int list2max = targetnotcompared.count; for(int = 0; < list1max;i++) { if (i >= list2max) break; //if (sourcenotcompared[i].name == targetnotcompared[i].name) //{ // var result = sourcenotcompared[i].version.compareto(targetnotcompared[i].version); // if (result == 0) // { // sourcenotcompared[i].state = "same"; // targetnotcompared[i].state = "same"; // } // else // { // sourcenotcompared[i].state = "different"; // targetnotcompared[i].state = "different"; // } //} } // give compared list itemssource datagrid.itemssource = sourcenotcompared; datagrid1.itemssource = targetnotcompared;
try implement iequalitycomparer compare objects. pick common objects between lists using intersect. update state values in common list. have tried object in machine. please find below code, tried.
your object
public class foundassembly { public string version { get; set; } public string name { get; set; } public string state { get; set; } }
implementation of iequalitycomparer
public class mycustomcomparer : iequalitycomparer<foundassembly> { public bool equals(foundassembly x, foundassembly y) { if (object.referenceequals(x, y)) { return true; } if (x == null || y == null) return false; return x.name.equals(y.name) && x.version.equals(y.version); } public int gethashcode(foundassembly obj) { return obj.name.gethashcode() * obj.version.gethashcode(); } }
then linq find common object
list<foundassembly> first = new list<foundassembly>(); first.add(new mycheck.foundassembly { name = "a", version = "b" }); first.add(new mycheck.foundassembly { name = "a2", version = "b2" }); first.add(new mycheck.foundassembly { name = "l1", version = "l1" }); list<foundassembly> second = new list<foundassembly>(); second.add(new mycheck.foundassembly { name = "a", version = "b" }); second.add(new mycheck.foundassembly { name = "a2", version = "b2" }); var common = first.intersect(second, new mycustomcomparer()).tolist(); //to update value. common.select(c => { c.state = "same"; return c; }).tolist();
hope someone.
Comments
Post a Comment