Introduction to Operators in Linq

No.of Views558
Bookmarked0 times
Downloads 
Votes0
By  jalpesh   On  21 Jun 2010 10:06:00
Tag : LINQ , Miscellaneous
I have found three more new operators in Linq which is use full in day to day programming stuff. Take,Skip and Reverse. Here are explanation of operators how it works.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

I have found  new operators in Linq which is use full in day to day programming stuff. Take,Skip,Union,Except ,Intersect operator and Reverse,. Here are explanation of operators how it works.

Take Operator

 Take operator will return first N number of element from entities.

Skip Operator

Skip operator will skip N number of element from entities and then return remaining elements as a result.

Reverse Operator

As name suggest it will reverse order of elements of entities.

Sample Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication1
{class Program
  {static void Main(string[] args)
      {string[] a = { "a", "b", "c", "d" };
        

          Console.WriteLine("Take Example");
          var TkResult = a.Take(2);foreach (string s in TkResult)
          {
              Console.WriteLine(s);
          }

          Console.WriteLine("Skip Example");
          var SkResult = a.Skip(2);foreach (string s in SkResult)
          {
              Console.WriteLine(s);
          }

          Console.WriteLine("Reverse Example");
          var RvResult = a.Reverse();foreach (string s in RvResult)
          {
              Console.WriteLine(s);
          }
          
      }
  }
}

 

Output

Image Loading

Union Operator

Union operator will combine elements of both entity and return result as third new entities.

Except Operator

Except operator will remove elements of first entities which elements are there in second entities and will return as third new entities.

Intersect Operator

As name suggest it will return common elements of both entities and return result as new entities.

Sample

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ConsoleApplication1
{
 class Program
 {static void Main(string[] args)
     {string[] a = { "a", "b", "c", "d" };string[] b = { "d","e","f","g"};

         var UnResult = a.Union(b);
         Console.WriteLine("Union Result");foreach (string s in UnResult)
         {
             Console.WriteLine(s);           
         }

         var ExResult = a.Except(b);
         Console.WriteLine("Except Result");foreach (string s in ExResult)
         {
             Console.WriteLine(s);
         }

         var InResult = a.Intersect(b);
         Console.WriteLine("Intersect Result");foreach (string s in InResult)
         {
             Console.WriteLine(s);
         }
         Console.ReadLine();
        
     }

 }
}

 

in above code a simple console application as a example where i have used two string array and applied the three operator one by one and print the result using Console.Writeline. Here is the code for that.

Output

Image Loading

Hope this will help you.

 
Sign Up to vote for this article
 
About Author
 
jalpesh
Occupation-Software Engineer
Company-DotNetJaps
Member Type-Expert
Location-India
Joined date-08 May 2010
Home Page-http://www.dotnetjalps.com
Blog Page-http://www.dotnetjalps.com
I am jalpesh vadgamaa an Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having experience of five year in Microsoft .NET Technology.I am working as Project Leader in Mid Size company.My work area comprises of Enterprise Level projects using ASP.NET and other Microsoft .NET Technologies.Please feel free to contact me for any queries via posting comments on my blog I will try to reply as early as possible.
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top