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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:04:20+00:00 2026-06-14T23:04:20+00:00

I am trying to locate duplicate objects based on 2 fields but ONLY where

  • 0

I am trying to locate duplicate objects based on 2 fields but ONLY where a 3rd field is also null

ItemNumber, Name, Pricebook, Parent
Item1, A, B, <null>
Item2, A, B, Item1
Item3, A, B, <null>
Item4, A, B, Item1
Item5, A, B, Item2

So in the above list the only 2 duplicate items are effectively Item1 and Item3

var duplicateItemsList =
    from i in items
    group i by new { i.ItemNumber, i.Pricebook, i.Parent } into d
    where d.Count() > 1
    select new { ItemNumber = d.Key.ItemNumber, Pricebook = d.Key.Pricebook, Parent = d.Key.Parent, Count = d.Count() };

The trouble I am having is working the check for the null field value in the Linq query.

After the above Linq query I just want to end up with a list containing the duplicated ItemNumber and Pricebook field values.

  • 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-14T23:04:21+00:00Added an answer on June 14, 2026 at 11:04 pm

    I don’t think you need Parent property in result and in groping key, because it will have null value. Also you don’t need to specify property name for anonymous object if it is same as name of assigned property.

    var duplicateItemsList =
        from i in items
        where i.Parent == null
        group i by new { i.Name, i.Pricebook } into d
        where d.Count() > 1
        select new { 
                      d.Key.Name, 
                      d.Key.Pricebook, 
                      Count = d.Count()
                   };
    

    Also you can introduce new range variable to store items count in group. Then items count will be calculated only once:

    var duplicateItemsList =
        from i in items
        where i.Parent == null
        group i by new { i.Name, i.Pricebook } into d
        let groupItemsCount = d.Count()
        where groupItemsCount > 1
        select new { 
                      d.Key.Name, 
                      d.Key.Pricebook, 
                      Count = groupItemsCount
                   };
    

    UPDATE as @Blachshma pointed, you have grouping by ItemNumber instead of Name

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

Sidebar

Related Questions

I am trying to locate leaks using instruments, but the leaks I see there
I'm trying to locate only children that begin with the text EPCF from my
I spent a very long time trying to locate the error but I can't
In the Visual Studio IDE, I am trying to locate all static fields, the
I am trying to locate IOS SDK 4.0. But I'm unable to find it.
Possible Duplicate: Unable to locate tools.jar I'm trying to use ant to compile using
I am trying to locate a tag on a page, but so far finding
I am trying to locate in the HL7 Stream for the segment that will
I am trying to locate a key in a HashMap. I can print the
I'm trying to locate the number of matches in a relative path for directory

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.