Reputation: 6668
I have two lists that I wish to compare. I am trying to use the Except method to get a list of the different rows between the lists. However nothing appears to happen. I cannot see a value my list called missingAuto and in the immediate window it tells me the missingAuto list does not exist in the current context.
The bottom section of the code has my implementation of IEqualityComparer.
for(int i = 0; i < _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;
}
}
}
Upvotes: 0
Views: 73
Reputation: 56536
Works for me. The 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 you describe when debugging sounds like you built for Release mode, and some variables were optimized away. You should build for Debug when you plan to debug, so that things work the way you expect.
Upvotes: 2