User Id :    Password :      New Member   Forgot Password  
 
Distinct operator in Linq
Description Distinct operator in Linq   No. of Views     1446
  Rating     0
Author Jalpesh Vadgama   Posted On     07 Mar 2011
Tags ADO.NET,C#,LINQ,LINQ to Object    

Sample Code   Download Code

 

Linq operator provides great flexibility and easy way of coding. Let’s again take one more example of distinct operator. As name suggest it will find the distinct elements from IEnumerable. Let’s take an example of array in console application and then we will again print array to see is it working or not. Below is the code for that. In this application I have integer array which contains duplicate elements and then I will apply distinct operator to this and then I will print again result of distinct operators to actually see whether its working or not.

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

namespace Experiment
{   
	class Program   
	{       
		static void Main(string[] args)       
		{           
			int[] intArray = { 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5 }; 
		        var uniqueIntegers = intArray.Distinct();           
			foreach (var uInteger in uniqueIntegers)           
			{               
				Console.WriteLine(uInteger);           
			}           
			Console.ReadKey();        
		}   
	}
}

Below is output as expected..
 

That’s cool..Stay tuned for more.. Happy programming.
 

About Author

About Author I am jalpesh vadgama an Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having more then 5 years of experience in .NET Technology.Please feel free to contact me for any queries via posting comments on my blog I will try to reply as early as possible. Please also visit my blog at http://jalpesh.blogspot.com/ Jalpesh Vadgama
No Photo
 
Country India
Company N/A
Home Page http://jalpesh.blogspot.com/

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

   
Write your comment here.
Comment
Verification Code