Click here to Skip to main content
Click here to Skip to main content

Linq Except Operator to Get NonEmployee And Employee collection

By , 17 Dec 2012
 

Introduction

There are two set collection of one ResourceBased Collection and other EmployeeBasedCollection. ResourceBased collection is superset and EmployeeBasedCollection is subset. Using the LINQ Except operator i will try to fetch NonEmployeeBasedCollection.


Background

Usually we carry set based operation in SQL query at database level using joins and Except operator in database. What if we got the similar case in code behind or layered architecture where we got two set collections in hand and where we don't want to involve database. In such cases generics and LINQ comes handy. This is what I tried to demonstrated below .

Using the code

The code is self explanatory.

 
 
 
 
protected void Page_Load(object sender, EventArgs e)
    {
        IList<ResourceBase> li=GetJoinAAndB();
        foreach (ResourceBase Ui in li)
        
            Response.Write(Ui.ID);
        
private IList<ResourceBase> GetJoinAAndB()
    {
      List<ResourceBase> resourceBaseSet = new List<ResourceBase>();
        resourceBaseSet.Add(
new ResourceBase(1,"santosh","sanAddress"));
 
 
        resourceBaseSet.Add(
new ResourceBase(2, "santosh", "sanAddress"));
        resourceBaseSet.Add(
new ResourceBase(3, "santosh", "sanAddress"));
List<ResourceBase> employeeBasedSet = new List<ResourceBase>();
        employeeBasedSet.Add(
new ResourceBase(1, "santosh", "sanAddress"));
        employeeBasedSet.Add(
new ResourceBase(4, "santosh", "sanAddress"));
        employeeBasedSet.Add(
new ResourceBase(5, "santosh", "sanAddress"));  
var query=( from a in resourceBaseSet                          
select a ).Except                               
from b in employeeBasedSet                                
from a1 in resourceBaseSet
where b.ID == a1.ID                                
select a1 );
 
return query.ToList();
    }
public class ResourceBase
    {
public ResourceBase(int id, string name, string address)
        {
            ID = id;
            Name = name;
            Address = address;
 
        }
public int ID { get; set; }
public String Name { get; set; }
public String Address { get; set; }
    }

     





   

License

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

About the Author

santosh poojari
Technical Lead
India India
He is presently working as tech arch in one of the leading IT company.He has total 10 years of experience in C#.net. He is a B.E graduate in Computers from Bombay University.
 
Most of his experiences are in designing architect for end to end solutions. His interest areas are WCF,Spring.net,Architecture- Model View Presenter,UML,Webservice,Performance Engineering/tuning,Design patterns,Generics,Enterprise Library,Regular expressions,Silverlight and WWF.
www.santoshpoojari.blogspot.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
SuggestionA lot of linq when a simple IEqualityComparer would have done PinmemberRobert Slaney18-Dec-12 9:16 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130619.1 | Last Updated 17 Dec 2012
Article Copyright 2012 by santosh poojari
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid