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

  • SEARCH
  • Home
  • 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 389841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:54:22+00:00 2026-05-12T15:54:22+00:00

LINQ Groupby query creates a new group for each unique key. I would like

  • 0

LINQ Groupby query creates a new group for each unique key. I would like to combine multiple groups into a single group based on the key value.

e.g.

var customersData = new[] 
{
    new { id = 1, company = "ABC" },
    new { id = 2, company = "AAA" },
    new { id = 3, company = "ABCD" },
    new { id = 4, company = "XYZ" },
    new { id = 5, company = "X.Y.Z." },
    new { id = 6, company = "QQQ" },
};

var groups = from d in customersData 
             group d by d.company;

Let’s say I want ABC, AAA, and ABCD in the same group, and XYZ, X.Y.Z. in the same group.

Is there anyway to achieve this through LINQ queries?

  • 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-12T15:54:23+00:00Added an answer on May 12, 2026 at 3:54 pm

    You will need to use the overload of GroupBy that takes an IEqualityComparer.

    var groups = customersData.GroupBy(k => k.company, new KeyComparer());
    

    where KeyComparer could look like

    
    public class KeyComparer : IEqualityComparer
    {
        public bool Equals(string x, string y)
        {
            // put your comparison logic here 
        }
    
        public int GetHashCode(string obj)
        {
            // same comparison logic here
        }
    }
    

    You can comparer the strings any way you like in the Equals method of KeyComparer.

    EDIT:

    You also need to make sure that the implementation of GetHashCode obeys the same rules as the Equals method. For example if you just removed the “.” and replaced with “” as in other answers you need to do it in both methods like this

    
    public class KeyComparer : IEqualityComparer
    {
        public bool Equals(string x, string y)
        {
            return x.Replace(".", "") == y.Replace(".", "");
        }
    
        public int GetHashCode(string obj)
        {
            return obj.Replace(".", "").GetHashCode();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to make NameValueCollection accessible to LINQ query operator such as where, join, groupby?
Using LINQ to Entities sounds like a great way to query against a database
Is it possible using Linq to create a group where items fall into more
I'm trying to convert a Linq query to SQL. My Linq query looks like
I'm stuggling to replicate a SQL query into LINQ. Can any one help? SQL:
How to write a LINQ query that would return row of type Value with
Everything I can find in linq for aggregation has a group by clause. How
This Linq to SQL query ... Return (From t In Db.Concessions Where t.Country =
Can LINQ to SQL query using NOT IN ? e.g., SELECT au_lname, state FROM
I'm using LINQPad to learn LINQ and I've run into a stumbling block. The

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.