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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:11:09+00:00 2026-05-12T20:11:09+00:00

i am trying to do a groupby in linq, basically i have a list

  • 0

i am trying to do a groupby in linq, basically i have a list ( along list – around 1000 entries) and i wish to groupby Description.

The entries are vehicles, so hence there are 50 or so Ford Mondeos

My query is pretty simple, no joins (yet 🙂 ) but it brings back a list including 50 Ford Mondeos, i wanted it to group them so there is only 1 entry.

I am only selecting Description, i am not selecting the IDs which would be different, but in LinqPad it returns the desc and i can see 50 ford mondeos that are all the same in description – letter for letter.

What am i doing wrong?

from v in dc.Vehicles
   group v by v.Description into g1
from y in g1
   orderby y.Description 
select new
{
     Desc = y.Description 
};

EDIT

It now brings back just 1 record for each ford mondeo, this was my test to ensure it worked but i need to expand on this, again it should only bring back 1 record each for ford mondeo as i have checked they all have same number of doors, categor, model id etc..

from v in dc.Vehicles
  group v by v.Description into g1
orderby g1.Key
  select new
  {
     Desc = g1.Key,
     CategoryId = g1.CategoryId,
     MakeId = g1.MakeId,
     ModelId = g1.ModelId,
     Doors = g1.Doors,
  };

Of the course the above doesn’t work it doesn’t find all the other fields i.e. CategoryId… i tried separating the group by and adding a comma for the other fields..

I think i have a little confusion over the key, i understand that this is the key but if you are grouping on more than 1 fields then potentially you would have more than 1 key..

Any ideas?

  • 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-12T20:11:09+00:00Added an answer on May 12, 2026 at 8:11 pm

    You’re using a second “from” which is causing the problem. Try this:

    from v in dc.Vehicles
    group v by v.Description into g1
    orderby g1.Key
    select new
    {
         Desc = g1.Key
    };
    

    Conceptually, a group like this consists of a “sequence of sequences” – one subsequence per group, with all the items matching that group. Each subsequence has a key – the description in this case.

    Your “from” clause was basically saying, “for each group, get me all the elements in the group” – effectively ungrouping them again 🙂 Typically you’d perform some sort of aggregation on the group, e.g. for the final part:

    select new
    {
         Desc = g1.Key,
         Oldest = g1.Min(x => x.Date),
         Newest = g1.Max(x => x.Date)
    };
    

    EDIT: To group on more than one field, use an anonymous type:

    group v by new { v.Description, v.Doors, v.Model } into g1
    

    That anonymous type will then form the key.

    To get any field within a record, you could try:

    select new
    {
    RandomField = g1.First().RandomField,
    Description = g1.Key,
    // etc
    }

    That logically takes the first record in the subsequence. I don’t know whether it’ll work for LINQ to SQL though – I know it would in LINQ to Objects, but I’ve no idea what the SQL equivalent would be. Even if it does work, it may well be incredibly expensive. I suspect that grouping by a composite key (via an anonymous type) is the way to go here.

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

Sidebar

Related Questions

I am trying to apply a bit of groupby/crosstabbing logic to an IEnumerable list
I'm trying to figure out the behavior of the library function groupBy (from Data.List),
I have the SQL and trying to convert ti to Linq query. I need
I'am trying to use LINQ to create a grouped list of documents based on
I have a collection List<CustomClass> which I am trying to find a way to
List<int> types = Hospitals.GroupBy(h => h.TypeId, (key, group) => group.First()) .Select(t=> y.TypeId).ToList(); Trying to
I'm trying to do a LINQ query to a list of RelatedProducts objects. I
Trying to work with groupby so that I can group together files that were
(source: yfrog.com ) I am trying my hand at making an invaders clone. there
I'm trying to understand how LINQ can be used to group data by intervals

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.