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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:26:11+00:00 2026-06-06T23:26:11+00:00

I want to compare two collections in C# that I’m currently doing using nested

  • 0

I want to compare two collections in C# that I’m currently doing using nested “for” loop. is there a way in Linq to do the same which will be quicker and more efficient? here is my current code which works perfectly just looking for an efficient way:

OrgCollection myYears = Org.RetrieveDistinctYear();
if (myYears.Count > 0)
{
AcademicYearCollection allYears = AcademicYear.RetrieveAll();
for (int i = 0; i < myYears.Count; i++)
{
    for (int j = 0; j < allYears.Count; j++)
    {
        if (myYears[i].AcademicYearCode == allYears[j].Code)
        {
        ddlYear.Items.Insert(0, new ListItem(allYears[j].Name,allYears[j].Code));
        break;
        }
    }
}
}

I want to compare “Code” from AcademicYearCollection with the “AcademicYearCode” property in OrgCollection & if it is the same then add it in the Dropdownlist “ddlYear”.

Thanks 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-06-06T23:26:12+00:00Added an answer on June 6, 2026 at 11:26 pm

    You can do it in LINQ, which gives shorter code. To know if it is more efficient or not you would have to profile it. I think that linq’s join operator uses some kind of hash buckets inernally which should give better performance, especially if the collections are large. Your current solution is O(N^2) which will quickly degrade if the number of options increases.

    OrgCollection myYears = Org.RetrieveDistinctYear();
    AcademicYearCollection allYears = AcademicYear.RetrieveAll();
    
    var items = from y in myYears
                join ay in allYears
                on y.Code equals ay.AcademicYearCode
                select new { ay.Name, ay.Code }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to compare two objects that are derived from the same base type
I would like to compare two columns in the same table. I want to
I want to compare two List of different lengths and/or same length. The first
I want to compare two images in Matlab (I learned that Matlab has more
I want to compare two routes to check if they are same or not
I have a method that I am currently building to compare two lists of
I want to compare two strings for equality when either or both can be
I want to compare two CSV files to see if they are different. I
I want to compare two times in VB.net: I have 1:42:21 PM and I
I want to compare two arrays, one coming from a shoppingcart and the other

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.