Click here to Skip to main content
Click here to Skip to main content
Go to top

Find Odds Out in a Generic List

, 22 Oct 2012
Rate this:
Please Sign up or sign in to vote.
Find the Odds Out in A Generic List

Introduction

The below article provides the solution for picking up the Odds out From a Generic List.

Background

Everyone who worked on Generic Lists might have faced the issue of retrieving the odds out from the list.

So I'm posting the solution here:

Using the code

For demonstrating this I have created three Data Transfer Object classes namely Student, Subject, and StudentSubjects:

  • Student - Student is a class with details of student like Student ID and Name.
  • Subject - Subject is a class with details of Subjects like Subject ID and Subject Name.
  • StudentSubjects - StudentSubjects is a class where we map Student with a Subject.

The classes declaration is as follows:

public class Student
{
    public Int32 SID { get; set; } // Student ID
    public String SName { get; set; }
}

public class Subject
{
    public Int32 SubjectID { get; set; } // Subject ID
    public String SubjectName { get; set; }
}

public class StudentSubjects
{
    public Int32 SubjectID { get; set; } // Subject ID
    public Int32 SID { get; set; } // Student ID
}

Assume that AllStudentsList contains all Students.

List<StudentSubjects> StudentSubjectsList = new List<StudentSubjects>(); 

Assume that  StudentSubjectsList contains list of the Students having a particular Subject, say Physics.

Now I want of Students who are not having their study Subject as Physics.

For achieving the odds out I'm using Lambda Expression.

var PhysicsStudents = StudentSubjectsList.Select(T => T.SID).ToList(); 

In the above line I have created a implicitly typed variable PhysicsStudents and selecting Student IDs into it from the list.

List<Student> OddStudents = AllStudentsList.Where(T => !PhysicsStudents.Contains(T.SID)).ToList();

And this is the code that gives you the odds out from the Student List. Here we are selecting Students which are not in Physics Students List.

If both the Lists are of same type you can use the below code using the keyword Except.

Assume that you have two Lists as below:

List<Student> AllStudentsList = new List<Student>();
List<Student> PhysicsStudentsList = new List<Student>();

To fetch the Odds out we can use the below code:

List<Student> OddStudents = AllStudentsList.Except(PhysicsStudentsList).ToList();

History

This is my first article, if any concerns please help me in bringing it out better.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Rajesh Kariyavula
Software Developer (Senior)
India India
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberSRIRAM 229-Oct-12 19:38 
GeneralMy vote of 5 PinmemberSavalia Manoj M22-Oct-12 20:52 
GeneralMy vote of 5 PinmemberVeda Prakash Behera22-Oct-12 18:27 
Excellent
SuggestionIf the lists are long, then this becomes quite inefficient PinmemberMatt T Heffron22-Oct-12 9:06 
GeneralRe: If the lists are long, then this becomes quite inefficient PinmemberRajesh Kariyavula22-Oct-12 18:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 22 Oct 2012
Article Copyright 2012 by Rajesh Kariyavula
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid