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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:58:40+00:00 2026-06-08T09:58:40+00:00

I am trying to perform a group by followed by a distinct to verify

  • 0

I am trying to perform a group by followed by a distinct to verify if a set of columns maps to only other column. For example, in the dataset below

Brand   Product      Location      Customer      Demand
 Box       A         Chicago       Chicago        10
 Box       B         Chicago       Milwaukee      20
 Cart      C         Madison       Milwaukee      10
 Cart      D         Chicago       Milwaukee      15

Product A, B, C are valid. But D is not valid since there exists a Product B with Chicago as Location & Milwaukee as Customer.
I am trying to build a LINQ query to get the exception record and having some trouble. I am very sure I have overcomplicated my query.

var vDuplicateSupplierLitho = from p in vRecords
                              group p by new
                              {
                                  Location = p["Location"].Cast<string>(),
                                  Customer = p["Customer"].Cast<string>()
                              } into grp
                              select new
                              {
                                  Location = grp.Key.Location,
                                  Customer = grp.Key.Customer,
                                  Products = from a in grp
                                             group a by new { Product = a["Product"].Cast<string>() } into apngrp
                                             where apngrp.Count() > 1 && apngrp.Select(a => a["Product"]).Distinct().Count() > 1
                                                  from NonUniqueRecord in apngrp
                                                  select new
                                                  {
                                                      Product = apngrp.key.Product,
                                                      Location = g.key.Location,
                                                      Customer = g.key.Customer
                                                      Demand = NonUniqueRecord["Demand"]
                                                  }
                                              };

Thanks.

  • 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-08T09:58:43+00:00Added an answer on June 8, 2026 at 9:58 am
    new[] {
            new { Brand = "Box", Product = "A", Location = "Chicago", Customer = "Chicago", Demand = 10 },
            new { Brand = "Box", Product = "B", Location = "Chicago", Customer = "Milwaukee", Demand = 20 },
            new { Brand = "Cart", Product = "C", Location = "Madison", Customer = "Milwaukee", Demand = 10 },
            new { Brand = "Cart", Product = "D", Location = "Chicago", Customer = "Milwaukee", Demand = 15 },
            new { Brand = "Cart", Product = "C", Location = "Madison", Customer = "Milwaukee", Demand = 10 }
            }
        .GroupBy(o => new { o.Location, o.Customer })
        .Where(g => g.Select(o => o.Product).Distinct().Count() > 1)
        .SelectMany(g => g)
    

    … or, in query syntax …

    from record in new[] {
            new { Brand = "Box", Product = "A", Location = "Chicago", Customer = "Chicago", Demand = 10 },
            new { Brand = "Box", Product = "B", Location = "Chicago", Customer = "Milwaukee", Demand = 20 },
            new { Brand = "Cart", Product = "C", Location = "Madison", Customer = "Milwaukee", Demand = 10 },
            new { Brand = "Cart", Product = "D", Location = "Chicago", Customer = "Milwaukee", Demand = 15 },
            new { Brand = "Cart", Product = "C", Location = "Madison", Customer = "Milwaukee", Demand = 10 }
            }
    group record by new { record.Location, record.Customer } into grouping
    where (from o in grouping select o.Product).Distinct().Count() > 1
    from duplicate in grouping
    select duplicate
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to perform a group by action on an aliased column (example below)
I'm trying to perform a DISTINCT clause on a query like this SELECT DISTINCT
I'm trying to perform a select query over two tables, one containing, for example,
I am trying to perform a query which groups a set of data by
I'm trying to create a (sqlite) query that will perform a GROUP BY but
I am trying to perform a SELECT query using a GROUP BY clause, however
I am trying to perform auto scaling on Amazon EC2 with below commands: elb-create-lb
I'm trying perform a relatively basic TCP socket send/receive operation on Windows CE using
Im trying to perform a mod rewriting to my web site. And I used
I am trying to perform an automatic login when the user clicks a link

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.