c# - Comparing Lists using Except method -
i have 2 lists wish compare. trying use except method list of different rows between lists. nothing appears happen. cannot see value list called missingauto , in immediate window tells me missingauto list not exist in current context.
the bottom section of code has implementation of iequalitycomparer.
for(int = 0; < _fundcodes.count; i++) { _hldlistauto = db.checkholdings(dtupload, "holdings", _fundcodes[i]); _hldlistman = db.checkholdings(dtupload, "holdings_live", _fundcodes[i]); // search through mannual list first list<holding> missingauto = _hldlistman.except(_hldlistauto, new holding.comparer()).tolist(); list<holding> missingman = _hldlistauto.except(_hldlistman, new holding.comparer()).tolist(); } class stockdetail { public string idsearch { get; set; } public string idsedol { get; set; } public double price { get; set; } public string name { get; set; } } class holding : stockdetail { public string fundcode { get; set; } public string currency { get; set; } public double nominal { get; set; } public class comparer : iequalitycomparer<holding> { public bool equals(holding x, holding y) { return x.fundcode == y.fundcode && x.idsedol == y.idsedol && x.nominal == y.nominal && x.currency == y.currency; } public int gethashcode(holding obj) { int hash = 17; hash = hash * 23 + (obj.fundcode ?? "").gethashcode(); hash = hash * 23 + (obj.idsedol ?? "").gethashcode(); hash = hash * 23 + (obj.nominal).gethashcode(); hash = hash * 23 + (obj.currency ?? "").gethashcode(); return hash; } } }
works me. following code:
list<holding> _hldlistauto = new list<holding> { new holding { fundcode = "asdf" }, new holding { fundcode = "qwer" }, }; list<holding> _hldlistman = new list<holding> { new holding { fundcode = "qwer" }, new holding { fundcode = "zxcv" }, }; list<holding> missingauto = _hldlistman.except(_hldlistauto, new holding.comparer()).tolist(); list<holding> missingman = _hldlistauto.except(_hldlistman, new holding.comparer()).tolist(); foreach (var holding in missingauto) console.writeline("missing auto " + holding.fundcode); foreach (var holding in missingman) console.writeline("missing man " + holding.fundcode);
outputs:
missing auto zxcv missing man asdf
the behavior describe when debugging sounds built release mode, , variables optimized away. should build debug when plan debug, things work way expect.
Comments
Post a Comment