Click here to Skip to main content
15,884,176 members
Articles / Programming Languages / C# 3.5
Tip/Trick

Find Odds Out in a Generic List

Rate me:
Please Sign up or sign in to vote.
4.86/5 (6 votes)
22 Oct 2012CPOL1 min read 10.7K   2   5
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:

C#
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.

C#
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.

C#
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.

C#
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:

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

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

C#
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)


Written By
Technical Lead
India India
Rajesh is an avid programmer with around 9 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.

Comments and Discussions

 
GeneralMy vote of 5 Pin
SRIRAM 229-Oct-12 19:38
SRIRAM 229-Oct-12 19:38 
GeneralMy vote of 5 Pin
Savalia Manoj M22-Oct-12 20:52
Savalia Manoj M22-Oct-12 20:52 
GeneralMy vote of 5 Pin
Ved Prakash Behera22-Oct-12 18:27
Ved Prakash Behera22-Oct-12 18:27 
SuggestionIf the lists are long, then this becomes quite inefficient Pin
Matt T Heffron22-Oct-12 9:06
professionalMatt T Heffron22-Oct-12 9:06 
GeneralRe: If the lists are long, then this becomes quite inefficient Pin
Rajesh Kariyavula22-Oct-12 18:03
Rajesh 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.