2

我有一个非常烦人的错误,我的 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
    }

当我运行调试器时,我在构造函数中得到了命中,但在比较方法中没有任何内容,有人可以帮忙吗??????

4

4 回答 4

0

从我在评论中读到的内容来看,您似乎需要一个IComparer<Account>实现,而不需要foreach您知道的那些循环。

那么,如果Accounts是 a List<Account>,你可以做

  Household household = query.First();
  household.Accounts.Sort(new AccountComparer());
  return household;

当然,如果Accounts不是 a List<Account>,则需要使用不同的方法对其进行排序,但总体思路是相同的。

于 2013-09-23T15:15:55.360 回答
0

感谢您的所有输入家伙,

我尝试了您的一些建议,发现最好的方法是直接对家庭内的帐户进行排序,而不是尝试对家庭进行排序以对帐户进行排序。这意味着我必须将帐户的返回类型更改为列表,然后按如下方式执行搜索:

        //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
    }

感谢你的帮助!!!

于 2013-09-23T15:18:30.943 回答
0

我知道三个可能的原因:

  1. 列表只有一个元素
  2. 列表为空
  3. 列表中的所有项目都返回不同的哈希码(GetHashCode)(这就是例如 Distinct 的工作方式)
于 2013-09-23T15:08:34.860 回答
-1

看看这个例子。该方法被声明为静态的并且直接传递给排序调用而不创建单独的类。另一种选择是让您的家庭类实现 IComparer 接口。然后您不必将任何内容传递给 Sort 方法,它将使用实现 IComparer 的 Compare 方法。

于 2013-09-23T14:58:00.387 回答