Arkiliknam
Arkiliknam

Reputation: 1825

LINQ GroupBy object or only anonymous types?

I know LINQ can group by taking advantage of anonymous types, so I though it would also group by if I used my own object for grouping. However, it is not (at least in my usage).

for example, using the following anonymous type groups by the two parameters I want it to:

.GroupBy(v => new { v.GroupId })

But once I use my own object, it no longer does the grouping:

.GroupBy(v => new MyGrouping { GroupId = v.GroupId })

With the following object

 private class MyGrouping : IMyGrouping
 {
     public int GroupId { get; set; }

     public override bool Equals(object obj) { return Equals((MyGrouping)obj); }
     public bool Equals(MyGrouping obj)
     {
         return this.GroupId == obj.GroupId ;
     }
 }

Am I missing something in my own object, or is this not supported?


As every poster pointed out, my Equals implementation was flawed so I revised it using Resharper recommended equality check just to be different and its grouping as expected.

 private class MyGrouping : IMyGrouping
 {
     public int GroupId { get; set; }

     public override bool Equals(object obj)
     {
         var myGrouping = obj as MyGrouping;
         return myGrouping != null ? Equals(myGrouping) : false;
     }
     public bool Equals(MyGrouping other)
     {
         if (ReferenceEquals(null, other)) return false;
         if (ReferenceEquals(this, other)) return true;
         return other.GroupId == GroupId;
     }

     public override int GetHashCode()
     {
         return GroupId;
     }
 }

For me though, this is a bit of a failuer as my goal was to cut down code... my previous implementation was grouping by a dynamic variable, so I may reconsider this now.

Upvotes: 2

Views: 340

Answers (2)

Justin Niessner
Justin Niessner

Reputation: 245469

There are definitely some issues with your MyGrouping class.

First, your overriden Equals method has infinite recursion if it is ever called because you never cast the object to your specific type. Second, you don't override the GetHashCode() method (which you should always do if you override Equals()). Third, if you're implementing a custom Equals method for your specific type, you should also add the IEquatable<T> interface to your class.

My class would look something like:

private class MyGrouping : IMyGrouping, IEquatable<IMyGrouping>
{
    public int GroupId { get; set; }

    public override bool Equals(object obj)
    {
        if(obj == null) return false;
        if(GetType() != obj.GetType()) return false;
        return Equals(obj as MyGrouping);
    }

    public override bool GetHashCode()
    {
        return GroupId.GetHashCode();
    }

    public bool Equals(IMyGrouping obj)
    {
        if(obj == null) return false;
        return GroupId == obj.GroupId;
    }
}

Upvotes: 4

watbywbarif
watbywbarif

Reputation: 7017

First of all your override of Equals is bad, first try with:

public override bool Equals(object obj)
{
  if (obj == null)
     return false;
  if (this.GetType() != obj.GetType())
     return false;
  return this.GroupID == ((MyGrouping)obj).GroupId;
}

Upvotes: 1

Related Questions