LINQ按自定义类型分组不起作用

时间:2014-07-15 14:17:23

标签: c# linq linq-group

我通过HTTPWebRequest从网络服务收到数据。在我使用NewtonSoft.Deserialize将其解析为自定义类型(具有公共字符串属性的简单类)之后,我想使用LINQ来操作此数据 - 更具体地说,我想对数据进行分组。

我的问题是如果我按单个字符串属性

分组,分组工作正常
from x in myList
group x by x.myStr into grp
select grp;

由于我想按更多列进行分组,因此我将返回一个带有

的自定义类型
new MyType { a = ..., b = ... }
然而,该小组没有工作。我认为原因必须是编译器不知道如何比较这些对象 - 所以如果这种类型实现IEqualityComparer<MyType>它将解决它。

但不,它仍然没有相应的分组,并且它创建了几个具有完全相同字符串值的键。

我分组的自定义类型类似于

public class MyType
{
    public string a;
    public string b;
    public string c;
}

我缺少什么想法?

以下是上述情景的具体示例:

//The type that models the data returned from the web service
public class MyClass
{
    public string a { get; set; }

    public string b { get; set; }

    public string c { get; set; }

    public DateTime d { get; set; }

    public DateTime e { get; set; }
}

// the type by which I want to group my data
public class MyGroup : IEquatable<MyGroup>, IEqualityComparer<MyGroup>
{
    public string f1 { get; set; }

    public DateTime d1 { get; set; }

    public DateTime d2 { get; set; }

    public bool Equals(MyGroup other)
    {
        return string.Compare(this.f1, other.f1) == 0;
    }

    public bool Equals(MyGroup x, MyGroup y)
    {
        return string.Compare(x.f1, y.f1) == 0;
    }

    public int GetHashCode(MyGroup obj)
    {
        return obj.GetHashCode();
    }
}    
    List<MyClass> l = new List<MyClass>();
    l.Add(new MyClass { a = "aaa", b = "bbb", c = "ccc", d = DateTime.ParseExact("20081405", "yyyyddMM", Thread.CurrentThread.CurrentCulture), e = DateTime.ParseExact("20140101", "yyyyddMM", Thread.CurrentThread.CurrentCulture) });
    l.Add(new MyClass { a = "aaaa", b = "bbb", c = "ccc", d = DateTime.ParseExact("20090105", "yyyyddMM", Thread.CurrentThread.CurrentCulture), e = DateTime.ParseExact("20140201", "yyyyddMM", Thread.CurrentThread.CurrentCulture) });
    l.Add(new MyClass { a = "aa", b = "bbbb", c = "cccc", d = DateTime.ParseExact("20081405", "yyyyddMM", Thread.CurrentThread.CurrentCulture), e = DateTime.ParseExact("20140201", "yyyyddMM", Thread.CurrentThread.CurrentCulture) });
    l.Add(new MyClass { a = "aaa", b = "bbbbb", c = "ccc", d = DateTime.ParseExact("20121111", "yyyyddMM", Thread.CurrentThread.CurrentCulture), e = DateTime.ParseExact("20140101", "yyyyddMM", Thread.CurrentThread.CurrentCulture) });
    l.Add(new MyClass { a = "aaaaa", b = "bbb", c = "ccc", d = DateTime.ParseExact("20081405", "yyyyddMM", Thread.CurrentThread.CurrentCulture), e = DateTime.ParseExact("20140101", "yyyyddMM", Thread.CurrentThread.CurrentCulture) });
    l.Add(new MyClass { a = "aaaa", b = "bbbbb", c = "ccc", d = DateTime.ParseExact("20121111", "yyyyddMM", Thread.CurrentThread.CurrentCulture), e = DateTime.ParseExact("20140101", "yyyyddMM", Thread.CurrentThread.CurrentCulture) });
    l.Add(new MyClass { a = "aaaa", b = "bbbb", c = "cccccc", d = DateTime.ParseExact("20081405", "yyyyddMM", Thread.CurrentThread.CurrentCulture), e = DateTime.ParseExact("20140201", "yyyyddMM", Thread.CurrentThread.CurrentCulture) });
    l.Add(new MyClass { a = "aaaaa", b = "bbb", c = "cccc", d = DateTime.ParseExact("20090105", "yyyyddMM", Thread.CurrentThread.CurrentCulture), e = DateTime.ParseExact("20140301", "yyyyddMM", Thread.CurrentThread.CurrentCulture) });
    l.Add(new MyClass { a = "aaa", b = "bbb", c = "cccc", d = DateTime.ParseExact("20081405", "yyyyddMM", Thread.CurrentThread.CurrentCulture), e = DateTime.ParseExact("20140201", "yyyyddMM", Thread.CurrentThread.CurrentCulture) });

    //The following does not really group
    //IEnumerable<IGrouping<MyGroup, MyClass>> r = from x in l
    IEnumerable<IGrouping<string, MyClass>> r = from x in l
                                                //group x by new MyGroup { f1 = x.a /*, d1 = x.d, d2 = x.e*/ } into grp
                                                orderby x.a
                                                group x by x.a into grp
                                                select grp;

    //foreach (IGrouping<MyGroup, MyClass> g in r)
    foreach (IGrouping<string, MyClass> g in r)
    {
        //Console.WriteLine(g.Key.f1);

        Console.WriteLine(g.Key);
    }

3 个答案:

答案 0 :(得分:5)

  

我认为原因必须是编译器不知道如何比较这些对象 - 所以如果这种类型实现IEqualityComparer<MyType>它将解决它。

实际上,要使用自定义&#34;平等&#34;检查您需要实施的IEquatable<T> Linq函数。 IEquatable<T>用于将对象的实例与同一类型的另一个对象进行比较 - 而IEqualityProvider<T>旨在由外部类实现,以比较两个任意T s(和/或多种方法来确定&#34;平等&#34;)。

请注意,您还应该实施Object.EqualsObject.GetHashCode - IEquatable<T>,只允许您以类型安全的方式进行比较。

  

为什么需要覆盖Object EqualsGetHashCode

确保用于比较两个对象的任何方法(Object.Equals(object),静态Object.Equals(object, object等)是一致的。每当您覆盖Equals时,您还应覆盖GetHashCode以确保对象可以正确存储在基于哈希的集合中,例如DictionaryHashSet

  

IEquitable仅以类型安全的方式进行比较是什么意思?

使用IEquatable<T>时,您正在比较的对象保证T(或T的子类型),而使用Object.Equals,您不知道其他对象的类型,必须先检查它的类型。

例如:

// IEquatable<T>.Equals()
public bool Equals(MyGroup other)
{
    return string.Compare(this.f1, other.f1) == 0;
}

// Object.Equals()
public bool Equals(object other)
{
    // need to check the type of the passed in object
    MyGroup grp = other as MyGroup;

    // other is not a MyGroup
    if(grp == null return false);        

    return string.Compare(this.f1, grp.f1) == 0;

    // you could also use
    //    return this.Equals(grp);
    // as a shortcut to reuse the same "equality" logic
}

答案 1 :(得分:3)

  

我缺少什么想法?

类似的东西:

public class MyType : IEquatable<MyType>
{
  public string a;
  public string b;
  public string c;

  public bool Equals(MyType other)
  {
    if (other == null)
      return false;

    if (GetType() != other.GetType()) // can be omitted if you mark the CLASS as sealed
      return false;

    return a == other.a && b == other.b && c == other.c;
  }

  public override bool Equals(object obj)
  {
    return Equals(obj as MyType);
  }

  public override int GetHashCode()
  {
    int hash = 0;
    if (a != null)
      hash ^= a.GetHashCode();
    if (b != null)
      hash ^= b.GetHashCode();
    if (c != null)
      hash ^= c.GetHashCode();
    return hash;
  }
}

添加:请注意,上面的MyType是可变的,如果重新分配其中一个字段abc,则哈希码会更改。如果在Dictionary<MyType, whatever>HashSet<MyType>等实例中进行重新分配,则会出现问题。


或者,您可以按照DavidG的回答中的建议“分组”匿名类型,或者“分组依据”Tuple.Create(.. , .. , ..)

答案 2 :(得分:1)

使用匿名类型进行分组。各个值本身看起来是字符串(即已经内置比较器的简单类型),因此除了返回之外,您不需要自定义类型。这样就无需担心IEqualityComparer

from x in myList
group x by new { x.myStr, x.otherStr, x.AnotherStr } into grp
select new MyTpe 
{
    a = grp.Key.myStr, 
    b = grp.Key.otherStr, 
    c = grp.Key.AnotherStr
};