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

The Archive Base Latest Questions

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

I have list of following class Student class student { Guid id; string name;

  • 0

I have list of following class Student

class student
{
   Guid id;
   string name;
}

The list contains multiple students. To search a student with specific id, I need to use foreach loop and compare id of each student.

I am looking for a better alternative instead of foreach loop. Is there any alternative available?

[EDIT]: What I meant by better alternative is optimized solution in terms of execution time and performance

[EDIT2] One more twist, what if id is Guid.

Thanks,

Ram

  • 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-31T09:21:08+00:00Added an answer on May 31, 2026 at 9:21 am

    Nothing will really change the fact that you have to iterate over the list. But you can use LINQ:

    List<Student> studentsList = ReadStudentsList();
    var student = studentsList.Where(s => s.id == ID_IM_LOOKING_FOR).Single();
    

    Based on an answer by @Fredrik Mörk, this could be shortened to:

    var student = studentsList.Single(s => s.id == ID_IM_LOOKING_FOR);
    

    Also note, that Single() will throw an exception, if no student is found. If you’d prefer to just return null, use SingleOrDefault().

    But what you actually want to be doing is storing your students in a map:

    Dictionary<int, Student> students = ReadStudentsMap();
    var student = students[ID_IM_LOOKING_FOR];
    

    This has a way better performance (O(1) for hashtables, O(log(n)) for trees) than looking through the list (O(n))!

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

Sidebar

Related Questions

I have the following class structure: class Organization { string Name; List<User> users; List<Organization>
I have the following code: class Employee { friend string FindAddr( list<Employee> lst,string name
I have the following C# class: public class SomeClass{ public string Name; public List<SomeClass>
I have the following ArrayList in my Action Class : List<Student> students = ArrayList<Students>();
I have the following list: class Person { public String Name { get; set;
Given that I have the following WCF service: class LookUpService { public List<County> GetCounties(string
I have a table which consists of the following Student Name Grade Class --------------------------------------------------------
I have the following class: public class LooklessControl : Control { public List<int> IntList
I have the following class structure class Top : NotifyPropertyChanged { private List<Inner> innerList;
I have a class like the following: class node { public: node* parent; std::list<node*>

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.