Linq ToDictionary匿名vs具体类型

本文关键字:类型 vs ToDictionary 匿名 Linq | 更新日期: 2023-09-27 18:17:38

我正在尝试使用Linq将列表转换为字典。我已经能够得到适当的结果与匿名类型作为关键,但不与具体类型。请参阅下面的代码片段:

        // 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可能比dictionary更适合;在处理完匿名类型后,我将查找查找。

Linq ToDictionary匿名vs具体类型

Dictionary使用Object。等号和对象。GetHashCode默认用于检查键。你不会想要IComparable (IComparable是关于排序事物,而不是检查它们是否相等。从技术上讲,仅仅因为CompareTo返回0,并不意味着两个对象是相同的)。您需要覆盖Object.GetHashCode()Object.Equals(object otherObject),这些匿名类型会自动执行,这就是它们在这里为您工作的原因。

=应该很容易,只要确保对象是正确的类型,然后检查两个字段是否相等。对于GetHashCode,在StackOverflow的其他地方有一些很好的答案(得到它可能有点棘手),比如这里:覆盖System.Object.GetHashCode的最佳算法是什么?