我有一个非常恼人的错误,因为我的comparer.Compare(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
}
当我运行调试器时,我在构造函数中获得了一个命中,但在compare方法中没有任何内容,任何人都可以帮助?????
答案 0 :(得分:0)
我知道三个可能的原因:
答案 1 :(得分:0)
根据我在评论中看到的内容,您似乎需要IComparer<Account>
实施,而不需要知道那些foreach
循环。
然后,如果Accounts
是List<Account>
,则可以
Household household = query.First();
household.Accounts.Sort(new AccountComparer());
return household;
当然,如果Accounts
不是List<Account>
,您需要使用不同的方法对其进行排序,但总体思路是相同的。
答案 2 :(得分:0)
感谢所有输入人员,
我尝试了一些你的建议,发现最好的建议是直接对家庭内的账户进行排序,而不是尝试对对账户进行分类的家庭进行排序。这意味着我必须将帐户的返回类型更改为List,然后按如下方式执行搜索:
//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
}
感谢您的帮助!!!
答案 3 :(得分:-1)
看看this example。该方法声明为static,并直接传递给sort调用,而不创建单独的类。另一个选择是让您的Household类实现IComparer接口。然后你不必向Sort方法传递任何东西,它将使用实现IComparer的Compare方法。