我有一个非常烦人的错误,我的比较器。比较(x, y)未被调用。 我有一个 IList,它从数据库中返回一堆实体,然后我对从数据库返回的每个实体内的实体列表进行排序。
IE:在此示例中,每个家庭都有许多帐户,我想按帐户实体的属性对子列表进行排序。
我的调用逻辑如下:
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
}
当我运行调试器时,我在构造函数中得到了命中,但在比较方法中没有任何内容,任何人都可以提供帮助?????
我知道三个可能的原因:
- 列表只有一个元素
- 列表为空
- 列表中的所有项目都返回不同的哈希代码(GetHashCode)(这就是例如Distinct 的工作方式)
从我在评论中读到的内容来看,看起来你需要一个IComparer<Account>
实现,没有你知道的那些foreach
循环。
然后,如果Accounts
是一个List<Account>
,你可以做
Household household = query.First();
household.Accounts.Sort(new AccountComparer());
return household;
当然,如果Accounts
不是List<Account>
,则需要不同的方法来对其进行排序,但总体思路是相同的。
感谢您的所有输入,
我尝试了您的一些建议,发现最好的建议是直接对家庭内的帐户进行排序,而不是尝试对家庭进行排序。 这意味着我必须将帐户的返回类型更改为列表,然后按如下方式执行搜索:
//sort the internal Accounts in memory.
List<Household> households = query.ToList();
foreach (var household in households)
household.Accounts.Sort(Compare);
return households;
比较器必须是:
public static int Compare(Account xAccount, Account yAccount)
{
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 (xAccount.StartDate > yAccount.StartDate) return 1;
if (xAccount.StartDate < yAccount.StartDate) return -1;
}
return 0; // it shouldn't get here
}
感谢您的帮助!!
看看这个例子。 该方法声明为 static ,并直接传递给排序调用,而无需创建单独的类。 另一种选择是使家庭类实现 IComparer 接口。 然后,您不必将任何内容传递给 Sort 方法,它将使用实现 IComparer 的 Compare 方法。