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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:06:24+00:00 2026-05-26T09:06:24+00:00

I have the following foreach loop: List<WorkingJournal> workingJournals = new List<WorkingJournal>(); foreach (WorkRoster workRoster

  • 0

I have the following foreach loop:

List<WorkingJournal> workingJournals = new List<WorkingJournal>();
foreach (WorkRoster workRoster in workRosters)
{
    bool exists = workingJournals.Any(workingJournal => workingJournal.ServicePlan.Id == workRoster.ServicePlan.Id
                                      && workingJournal.Nurse.Id == workRoster.Nurse.Id
                                      && workingJournal.Month == workRoster.Start.Month
                                      && workingJournal.Year == workRoster.Start.Year);

    if (exists == false)
    {
                WorkingJournal workingJournal = new WorkingJournal
                {
                    ServicePlan = workRoster.ServicePlan,
                    Nurse = workRoster.Nurse,
                    Month = workRoster.Start.Month,
                    Year = workRoster.Start.Year
                };

                workingJournals.Add(workingJournal);
    }
}

I started writing:

from workRoster in workRosters
select new WorkingJournal
                {
                    ServicePlan = workRoster.ServicePlan,
                    Nurse = workRoster.Nurse,
                    Month = workRoster.Start.Month,
                    Year = workRoster.Start.Year
                };

But now I am stuck with the comparison that produces distinct WorkingJournals.
I have a feeling that a group by clause should be here but I’m not sure how it should be done.

  • 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-26T09:06:25+00:00Added an answer on May 26, 2026 at 9:06 am

    If you have proper Equals and GetHashCode implementations inside your class, you can simply invoke Distinct().

    var result = workRosters.Select(...).Distinct();
    

    On the chance you do not have such implementations, you can define an IEqualityComparer<WorkingJournal> implementation. This will have you defining Equals and GetHashCode methods for the T that can then be used by a dictionary or hashset and can also be used in overloads of Distinct() in Linq.

    class JournalComparer : IEqualityComparer<WorkingJournal>
    {
        public bool Equals(WorkingJournal left, WorkingJournal right)
        {
             // perform your equality semantics here
        }
    
        public int GetHashCode(WorkingJournal obj)
        {
             // return some hash code here.
             return obj.ServicePlan.GetHashCode();
        }
    }
    
    var comparer = new JournalComparer(); // implements the interface 
    var result = workRosters.Select(r => new WorkingJournal { ... }).Distinct(comparer);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following exception in the foreach loop Unable to cast object of type
I have the following code: var items = new List<string> {1, 2, 3}; //
I have a foreach loop that cycles through a list of types and creates
I have referred to the following question at: Using foreach loop to iterate through
The following code returns the error from the For Each loop. I have similar
I have the following code foreach (DataRowView dr in Data) { if (dr ==
I have the following code: foreach (Tuple<Point, Point> pair in pointsCollection) { var points
I have the following code: foreach (var control in this.Controls) { } I want
I have the following code: foreach (ListItem item in check.Items) { if (item.Selected) {
Suppose I have the following code: foreach(string str in someObj.GetMyStrings()) { // do some

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.