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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:54:57+00:00 2026-06-05T05:54:57+00:00

Lets say I have a generic list of the the following objects: public class

  • 0

Lets say I have a generic list of the the following objects:

public class Supermarket
{
    public string Brand { get; set; }
    public string Suburb { get; set; }
    public string State { get; set; }
    public string Country { get; set; }
}

So using a List<Supermarket> which is populated with many of these objects with different values I am trying to:

  1. Select the distinct Suburb properties from a
    superset of Supermarket objects contained in a List<Supermarket> (say this superset contains 20
    distinct Suburbs).

  2. Join the Distinct List of Suburbs above to another set of aggregated and counted Suburbs obtained by a LINQ query to a different, smaller list of List<Supermarket>

The distinct items in my superset are:

"Blackheath"
"Ramsgate"
"Penrith"
"Vaucluse"
"Newtown"

And the results of my aggregate query are:

"Blackheath", 50
"Ramsgate", 30
"Penrith", 10

I want to join them to get

"Blackheath", 50
"Ramsgate", 30
"Penrith", 10
"Vaucluse", 0
"Newtown", 0

Here is what I have tried so far:

var results = from distinctSuburb in AllSupermarkets.Select(x => x.Suburb).Distinct()
                select new
                {
                    Suburb = distinctSuburb,
                    Count = (from item in SomeSupermarkets
                            group item by item.Suburb into aggr
                            select new
                            {
                                Suburb = aggr.Key,
                                Count = aggr.Count()
                            } into merge
                            where distinctSuburb == merge.Suburb
                            select merge.Count).DefaultIfEmpty(0)
                } into final
                select final;

This is the first time I have had to post on Stack Overflow as its such a great resource, but I can’t seem to cobble together a solution for this.

Thanks for your time

EDIT: OK So I solved this a short while after the initial post. The only thing I was missing was chaining a call to .ElementAtOrDefault(0) after the call to .DefaultIfEmpty(0). I also verifed that using .First() instead of .DefaultIfEmpty(0) as Ani pointed out worked, The correct query is as follows:

var results = from distinctSuburb in AllSupermarkets.Select(x => x.Suburb).Distinct()
                select new
                {
                    Suburb = distinctSuburb,
                    Count = (from item in SomeSupermarkets
                            group item by item.Suburb into aggr
                            select new
                            {
                                Suburb = aggr.Key,
                                Count = aggr.Count()
                            } into merge
                            where distinctSuburb == merge.Suburb
                            select merge.Count).DefaultIfEmpty(0).ElementAtOrDefault(0)
                } into final
                select final;

LASTLY: I ran Ani’s code snippet and confirmed that it ran successfully, so both approaches work and solve the original question.

  • 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-06-05T05:54:58+00:00Added an answer on June 5, 2026 at 5:54 am

    I don’t really understand the assumed equivalence between State and Suburb (where distinctSuburb == merge.State), but you can fix your query adding a .First() after the DefaultIfEmpty(0) call.

    But here’s how I would write your query: using a GroupJoin:

    var results = from distinctSuburb in AllSupermarkets.Select(x => x.Suburb).Distinct()
                  join item in SomeSupermarkets 
                            on distinctSuburb equals item.Suburb
                            into suburbGroup
                  select new
                  {
                        Suburb = distinctSuburb,
                        Count = suburbGroup.Count()
                  };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say we have a generic list of Class1, typically having ~100 objects for
Let's say I have the following class. MyClass<T> { public void MyMethod(T a, List<T>
For simplicity's sake, lets say I have the following Abstract Base Controller Class: public
Let's say I have the following list List<Invoice> InvoiceList [0] AdvertiserCustomerID = Generic Brand
Let's say I have the following class definition : List<SomeClass>... public class SomeClass {
Let's say I have a generic Object class, and a generic List class. I
Lets say have this immutable record type: public class Record { public Record(int x,
Lets say I have two classes one base class: public class BaseModel { }
Let's say I have a generic class: public class MyGenericClass<T> { ... } Now
Lets say that I have a generic collection in the string format. I want

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.