Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7544967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:48:34+00:00 2026-05-30T08:48:34+00:00

I know LINQ can group by taking advantage of anonymous types, so I though

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:48:35+00:00Added an answer on May 30, 2026 at 8:48 am

    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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Anybody know of a way to batch NHibernate queries using NHibernate.Linq like you can
I would like to know how to convert SQL query listed below into LINQ
I know LINQ doesn't have the SQL IN clause but rather uses contains. Can
Customer wants to move from SQL server to Sybase database. Don't know if LINQ
I know in normal Linq grammar, orderby xxx descending is very easy, but how
I often get in a position when I need to know why my LINQ
I know this could be done manually with some hardcoded Linq Joins. However, I
Anyone know why only undirectional serialization is supported in the Linq designer? Consider the
I know C# has the Random class and probably a few classes in LINQ
I'm using LINQ to Entities and I want to know how do I translate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.