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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:06:02+00:00 2026-05-18T01:06:02+00:00

I have a class… class Document { public int GroupID { get; set; }

  • 0

I have a class…

class Document
{
    public int GroupID  { get; set; }
    public bool Valid { get; set; }
    // more
}

… and a list of instances: IEnumerable<Document> documents. In a first step which runs object by object through this list those documents have been validated, which means: the property Valid will be true for some objects and false for other objects in the list.

Now in a second step I have to do the following:

  • If for at least one document per document group (defined by all documents with the same GroupID) the flag Valid is false then set Valid to false for all documents of the group.

To do this I have created so far the following code fragment:

var q = from d in documents
        group d by d.GroupID;
// q is now of type IEnumerable<IGrouping<int, Document>>

foreach (var dg in q) // dg = "document group", of type IGrouping<int, Document>
{
    if (dg.Any(d => !d.Valid))
    {
        foreach (var d in dg)
            d.Valid = false;
    }
}

I believe, this does what I want (I didn’t test it until now, though) but not very efficiently.

Question: Is there a way to improve this code, especially to move the semantics of the Any method in the outer foreach loop “somehow” into the initial LINQ query, so that q only represents the groups which have at least one invalid document? (Also I am obviously not interested in groups which have only one element, so those groups could be filtered out as well.)

Thank you for suggestions in advance!

  • 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-18T01:06:03+00:00Added an answer on May 18, 2026 at 1:06 am

    I think this does what you want:

    var q = from d in documents
            group d by d.GroupID into g
            where g.Count() > 1 && g.Any(d => !d.Valid)
            select g;
    
    foreach (var dg in q)
    {
        foreach (var d in dg)
        {
            d.Valid = false;
        }
    }
    

    The top part in fluent syntax would look like:

    var q = documents.GroupBy(d => d.GroupID)
                .Where(g => g.Count() > 1 && g.Any(d => !d.Valid));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class: public class MyClass { [XmlElement(Date)] public DateTime Date { get; set;
I have class method that returns a list of employees that I can iterate
I have class A: public class ClassA<T> Class B derives from A: public class
I have class with internal property: internal virtual StateEnum EnrolmentState { get { ..getter
I have: class Car {..} class Other{ List<T> GetAll(){..} } I want to do:
If I have class ObjA { public ObjB B; } class ObjB { public
I have an element like this: <span class=tool_tip title=The full title>The ful&#8230;</span> This seems
I have class Employee which is something like this. class Emp { int EmpID;
I have Class CustomDate and that is referred in other class called Test. public
I have class: public class EnglishWord implements Serializable, Comparable, Cloneable { static Logger logger

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.