c# - Compare Two Liste <T> -
how can compare 2 list ?
public class pers_ordre : iequalitycomparer<pers_ordre> { int _ordreid; public int lettrevoidid { { return _lettrevoidid; } set { _lettrevoidid = value; } } string _ordrecummul; public string ordrecummul { { return _ordrecummul; } set { _ordrecummul = value; } } // products equal if names , product numbers equal. public bool equals(pers_ordre x, pers_ordre y) { //check whether compared objects reference same data. if (object.referenceequals(x, y)) return true; //check whether of compared objects null. if (object.referenceequals(x, null) || object.referenceequals(y, null)) return false; //check whether products' properties equal. return x.lettrevoidid == y.lettrevoidid && x.ordrecummul == y.ordrecummul; } // if equals() returns true pair of objects // gethashcode() must return same value these objects. public int gethashcode(pers_ordre product) { //check whether object null if (object.referenceequals(product, null)) return 0; //get hash code name field if not null. int hashproductname = product.ordrecummul == null ? 0 : product.ordrecummul.gethashcode(); //get hash code code field. int hashproductcode = product.lettrevoidid.gethashcode(); //calculate hash code product. return hashproductname ^ hashproductcode; } }
and compare this:
private void simplebutton_comparer_click(object sender, eventargs e) { string lefile_client = system.io.path.combine(appdir, @"fa.csv"); string lefile_server = system.io.path.combine(appdir, @"fa_server.csv"); list<pers_ordre> olistclient = outils.getcsv(lefile_client).orderby(t => t.lettrevoidid).tolist(); list<pers_ordre> olistservert = outils.getcsvserver(lefile_server).orderby(t => t.lettrevoidid).tolist(); list<pers_ordre> lediff = new list<pers_ordre>(); lediff = olistservert.except(olistclient).tolist(); string noid = "", odreid = ""; foreach (var odiff in lediff) { noid += odiff.lettrevoidid + " "; odreid += odiff.ordrecummul + " "; } messagebox.show(noid + "--" + odreid); }
i can not right result.
the lists contain class objects , iterate through 1 list, looking same item in second list , report differences.
to object contains in list not in list b , vice versa.
your current .except()
call find items server missing on client, not find items on client missing on server.
try this:
private void simplebutton_comparer_click(object sender, eventargs e) { string lefile_client = system.io.path.combine(appdir, @"fa.csv"); string lefile_server = system.io.path.combine(appdir, @"fa_server.csv"); var listclient = outils.getcsv(lefile_client).orderby(t => t.lettrevoidid); var listserver = outils.getcsvserver(lefile_server).orderby(t => t.lettrevoidid); var lediff = listserver.except(listclient).concat(listclient.except(listserver)); var result = new stringbuilder(); foreach (var diff in lediff) { result.appendformat("{0} --{1} ", diff.lettrevoidid, diff.ordrecummul); } messagebox.show(noid.tostring() + "--" + odreid); }
this code should faster original, avoids loading results memory until builds final string. code in performs equivalent of 2 separate sql left joins. make faster still doing 1 full join, require writing our own linq operator method well.
Comments
Post a Comment