私の comparer.Compare(x, y) が呼び出されないという、本当に厄介なエラーがあります。データベースから一連のエンティティを返す IList があり、データベースから返された各エンティティ内のエンティティのリストを並べ替えています。
つまり、この例では、各世帯に多くのアカウントがあり、サブリストをアカウント エンティティのプロパティで並べ替えたいと考えています。
私の呼び出しロジックは次のとおりです。
List<Household> households = query.ToList();
households.Sort(new HouseholdComparer());
return households;
私の比較は次のようになります。
public class HouseholdComparer : IComparer<Household>
{
public int Compare(Household x, Household y)
{
foreach (Account xAccount in x.Accounts)
{
foreach (Account yAccount in y.Accounts)
{
if (xAccount.StartDate == yAccount.StartDate)
{
if ((xAccount.RevenueT12.HasValue && yAccount.RevenueT12.HasValue)
&& (xAccount.RevenueT12.Value == yAccount.RevenueT12.Value))
{
if ((xAccount.AUAAnnual.HasValue && yAccount.AUAAnnual.HasValue)
&& (xAccount.AUAAnnual.Value == yAccount.AUAAnnual.Value))
return 0; // all same whatever result
if (!xAccount.AUAAnnual.HasValue || !yAccount.AUAAnnual.HasValue) return 0;
if (xAccount.AUAAnnual.Value > yAccount.AUAAnnual.Value) return 1;
if (xAccount.AUAAnnual.Value < yAccount.AUAAnnual.Value) return -1;
}
else
{
if (!xAccount.RevenueT12.HasValue || !yAccount.RevenueT12.HasValue) return 0;
if (xAccount.RevenueT12.Value > yAccount.RevenueT12.Value) return 1;
if (xAccount.RevenueT12.Value < yAccount.RevenueT12.Value) return -1;
}
}
else
{
if (x.StartDate > y.StartDate) return 1;
if (x.StartDate < y.StartDate) return -1;
}
}
}
return 0; // it shouldn't get here
}
デバッガーを実行すると、コンストラクターでヒットが発生しますが、比較メソッドでは何もヒットしません。誰か助けてもらえますか?????