我有一个非常烦人的错误,我的 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
}
当我运行调试器时,我在构造函数中得到了命中,但在比较方法中没有任何内容,有人可以帮忙吗??????