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 605759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:10:50+00:00 2026-05-13T17:10:50+00:00

I have this class public class Item { public Coordinate coordinate { get; set;

  • 0

I have this class

public class Item
{
       public Coordinate coordinate { get; set; }
        ...
        ...
}

With Coordinate being define like this:

public class Coordinate
{
        public Coordinate(float latitude, float longitude)
        {
            Latitude = latitude;
            Longitude = longitude;
        }

        public float Latitude { get; private set; }
        public float Longitude { get; private set; }
}

And I want to have a linq query like that :

var grouped = from it in items
              group it by it.Coordinate into grp
              select grp;

As mentioned here by MSDN I thought this was possible if I would override Equals on my Coordinate class :

Use a named type if you must pass the
query variable to another method.
Create a special class using
auto-implemented properties for the
keys, and then override the Equals
and GetHashCode methods. You can also
use a struct, in which case you do not
strictly have to override those
methods. For more information see How
to: Implement an Immutable Class That
Has Auto-Implemented Properties

Equals implementation for Coordinate class :

public override bool Equals(object obj)
{
       var coord = obj as Coordinate;
       if(coord == null) return false;
       return (Latitude == coord.Latitude && Longitude == coord.Longitude);
}

still I cant get my linq query to group by similar coordinates, as my failing test shows :

[TestMethod]
public void GroupBy_3ItemsWith2DifferentCoordinates_Returns2Groups()
{
    var items = new List<Item>
        {
            new Item {Coordinate = new Coordinate(10, 10)},
            new Item {Coordinate = new Coordinate(10, 10)},
            new Item {Coordinate = new Coordinate(12, 10)},
        };
    var grouped = from it in items
                  group it by it.Coordinate into g
                  select g;
    Assert.AreEqual(2, grouped.Count());
}

There is an overload to the GrouBy method that takes an IEqualityComparer as a parameter, but is there the equivalent using the group clause?
Am I doing something wrong?? Any thoughts?

  • 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-13T17:10:51+00:00Added an answer on May 13, 2026 at 5:10 pm

    You’ve shown the Equals implementation, but not GetHashCode. You need to override both (and in a consistent way) for grouping to work.

    Sample GetHashCode implementation:

    public override int GetHashCode()
    {
        int hash = 23;
        hash = hash * 31 + Latitude.GetHashCode();
        hash = hash * 31 + Longitude.GetHashCode();
        return hash;
    }
    

    Note that comparing float values for exact equality is always somewhat risky – but I’d at least expect your unit tests to pass, given that they’re not performing any calculations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 299k
  • Answers 299k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There's no need to do anything - if your C… May 13, 2026 at 7:42 pm
  • Editorial Team
    Editorial Team added an answer Some more answers to your questions: 1) You're pretty much… May 13, 2026 at 7:42 pm
  • Editorial Team
    Editorial Team added an answer You currently can't modify the keyboards that the iPhone provides.… May 13, 2026 at 7:42 pm

Related Questions

I have 4 subclasses: Video , Image , Note , and Form . Each
In a webapp, i have a data-model (let's call it Item). The data of
I have a GridView on my aspx page which displays a collection of objects
I have a JAX-B java web service which I'm using to update a database.
I am new to design patterns and now I want to implement the Strategy

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.