Click here to Skip to main content
13,141,576 members (47,981 online)
Click here to Skip to main content
Add your own
alternative version


1 bookmarked
Posted 22 Oct 2012

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


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


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();


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


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


About the Author

Rajesh Kariyavula
Software Developer (Senior)
India India
Rajesh is an avid programmer with around 7 years of experience, who loves coding and loves to solve complex business problems. He started his career as a language trainer before pursuing his MCA and moved to Software Development.

He started his programming career with VB.Net developing Windows Applications and moved to Web Application development with C#.

His areas of expertise include C#.Net, ASP.Net, VB.Net, LINQ, LINQ to SQL, Entity Framework, WCF, NHibernate.

He is currently working with Apoorva Technologies as a Tech Lead.

Other than coding, Rajesh loves to cook experimenting on his favorite dishes.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
SRIRAM 229-Oct-12 19:38
memberSRIRAM 229-Oct-12 19:38 
GeneralMy vote of 5 Pin
Savalia Manoj M22-Oct-12 20:52
memberSavalia Manoj M22-Oct-12 20:52 
GeneralMy vote of 5 Pin
Veda Prakash Behera22-Oct-12 18:27
memberVeda Prakash Behera22-Oct-12 18:27 
SuggestionIf the lists are long, then this becomes quite inefficient Pin
Matt T Heffron22-Oct-12 9:06
memberMatt T Heffron22-Oct-12 9:06 
GeneralRe: If the lists are long, then this becomes quite inefficient Pin
Rajesh Kariyavula22-Oct-12 18:03
memberRajesh Kariyavula22-Oct-12 18:03 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170915.1 | Last Updated 22 Oct 2012
Article Copyright 2012 by Rajesh Kariyavula
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid