Distinct operator in Linq

Posted by Jalpesh P. Vadgama on ASP.net Weblogs See other posts from ASP.net Weblogs or by Jalpesh P. Vadgama
Published on Sat, 08 Jan 2011 19:41:48 GMT Indexed on 2011/01/08 19:54 UTC
Read the original article Hit count: 348

Filed under:
|
|
|

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

DisntictResult

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

Technorati Tags: ,
Shout it

© ASP.net Weblogs or respective owner

Related posts about c#.net

Related posts about LINQ