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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:58:17+00:00 2026-05-23T17:58:17+00:00

The following LINQ statement finds items with duplicate values for a particular property, groups

  • 0

The following LINQ statement finds items with duplicate values for a particular property, groups them by count then does another grouping to get a boolean back.

I’m just curious how this can be improved, it seems a bit wasteful and as this is part of a object model validation library I’d quite like to get it as speedy as I can.

Speed of execution is a priority, but any other suggestions are welcome.

var grouped = from g2 in
                 (from i in item.ParentList
                  where _filter(i)
                  group i by propGetter(i) into g
                  select new { Count = g.Count(), Items = g })
              group g2 by g2.Count == 1 into g3
              select new { IsUnique = g3.Key, Items = g3 };

foreach (var g in grouped)
{
    foreach (var grp in g.Items)
    {
        foreach (var itm in grp.Items)
        {
            if (g.IsUnique == false)
                itm.AddPropertyError(_propertyName, (int)Validations.Unique, _message);
            else
                itm.RemovePropertyError(_propertyName, (int)Validations.Unique);
        }
    }
}
  • 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-23T17:58:17+00:00Added an answer on May 23, 2026 at 5:58 pm

    As Oskar said, you probably don’t want to optimize the query for speed unless profiling shows that it’s causing a problem. Premature optimization is the root of all evil. If you want to optimize the query for readability, here is one way to simplify the second part:

    var items = grouped
        .SelectMany(group => group.Items)
        .SelectMany(group => group.Items)
    
    foreach (var item in items)
    {
        ...
    }
    

    Edit regarding your comment: Ah, I hadn’t noticed that you were referencing g.IsUnique inside the innermost loop. Here is one way to resolve that problem without 3 levels of indentation, but it may not be the best way:

    var uniqueItems = grouped
        .Where(group => group.IsUnique)
        .SelectMany(group => group.Items)
        .SelectMany(group => group.Items)
    
    var nonUniqueItems = grouped
        .Where(group => !group.IsUnique)
        .SelectMany(group => group.Items)
        .SelectMany(group => group.Items)
    
    foreach (var item in uniqueItems)
    {
        ...
    }
    
    foreach (var item in nonUniqueItems)
    {
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would the following sql statement translate to a linq query? select ID, Price,
What is the linq equivalent of the following statement ? IF NOT EXISTS(SELECT UserName
I've got the following LINQ Statement: Dim PSNum As Integer = 66 Dim InvSeq
is it possible to define a LINQ statement for the following problems WITHOUT using
Following linq statement generates multiple trips to the database, is there a way to
I have following LINQ statement: (from o in Table where (o.Username == username &&
I have the following linq statement builtIn = (from type in Assembly.GetExecutingAssembly().GetTypes() where type.IsClass
Imagine the following linq to observables statement: var x = from result1 in service1.operation()
I have the following simple LINQ to SQL statement: string strUserID = Eval(whoInsert).ToString(); insLusHmoobDataContext
I have the following linq expression that lets me join two tables, group them

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.