Linq ToDictionary匿名与具体类型

时间:2016-11-12 06:51:28

标签: c# linq linq-to-objects

我正在尝试使用Linq将列表转换为Dictionary。我已经能够以匿名类型作为键获得正确的结果,但不能使用具体类型。请参阅下面的代码:

        // Works. Produces 329 entries in dictionary, as expected. 
        var groupByMonth =
            from e in list
            group e by new { e.chk.Month, e.chk.Year } into g
            select new { month = g.Key, rates = g.ToList() };
        var monnthlyRates = groupByMonth.ToList();
        var monnthlyRatesDict = groupByMonth.ToDictionary(t => t.month, t => t.rates);

        // IS NOT WORKING. Produces 30K entries in dictionary; same num as in the list.
        // i.e. the grouping does not happen
        var groupByMonth2 =
            from e in list
            group e by new MonthYear { Month = e.chk.Month, Year = e.chk.Year } into g
            select new MonthYearRates { MonthYear = g.Key, Rates = g.ToList()};
        var monnthlyRatesDict2 = groupByMonth2.ToDictionary(t => t.MonthYear, t => t.Rates);

        // Works. Dictionary has 329 entries
        var groupByMonth3 =
            from e in list
            group e by new DateTime(e.chk.Year, e.chk.Month, 1) into g
            select new MonthYearRates2 { MonthYear = g.Key, Rates = g.ToList() };
        var monnthlyRatesDict3 = groupByMonth3.ToDictionary(t => t.MonthYear, t => t.Rates);

我尝试通过在具体类型中实现IComparer和/或IComparable来解决问题;无济于事

class MonthYear : IComparer
// class MonthYear : IComparable, IComparer
{
    public MonthYear()
    {
    }
    public MonthYear(int month, int year)
    {
        Month = month;
        Year = year;
    }
    int IComparer.Compare(Object x, Object y)
    {
        MonthYear xo = (MonthYear)x;
        MonthYear yo = (MonthYear)y;

        if (yo.Year > xo.Year)
            return 1;
        else if (yo.Year < xo.Year)
            return -1;
        else
        {
            if (yo.Month > xo.Month)
                return 1;
            else if (yo.Month < xo.Month)
                return -1;
            else
                return 0;
        }
    }

    //int IComparable.CompareTo(object obj)
    //{
    //    MonthYear o = (MonthYear)obj;

    //    if (Year > o.Year)
    //        return 1;
    //    else if (Year < o.Year)
    //        return -1;
    //    else
    //    {
    //        if (Month > o.Month)
    //            return 1;
    //        else if (Month < o.Month)
    //            return -1;
    //        else
    //            return 0;
    //    }
    //}
    public int Month;
    public int Year;
}

据我所知,Lookup可能更适合字典;完成任何类型后,我将查找Lookup。

1 个答案:

答案 0 :(得分:2)

Dictionary默认使用Object.Equals和Object.GetHashCode来检查键。你不需要IComparable(IComparable是关于订购的东西,而不是检查它们是否相等。从技术上讲,仅仅因为CompareTo返回零,并不意味着2个对象是相同的)。您需要覆盖Object.GetHashCode()Object.Equals(object otherObject),匿名类型会自动执行这些操作,这就是为什么它们适合您的原因。

等于应该很简单,只需确保对象是正确的类型,然后检查两个字段是否相等。对于GetHashCode,StackOverflow上的其他地方有一些很好的答案(让它变得有点棘手),就像这里:What is the best algorithm for an overridden System.Object.GetHashCode?