Thursday, March 29, 2012

Remove duplicate records from Generic List

Following code snippet give you a distinct values from the Generic int List. Here I'm using LINQ query to retrieve Distinct items

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

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List listOfItems = new List();

listOfItems.Add(4);
listOfItems.Add(2);
listOfItems.Add(3);
listOfItems.Add(1);
listOfItems.Add(6);
listOfItems.Add(4);
listOfItems.Add(3);

var duplicates = listOfItems
.GroupBy(i => i)
.Select(g => g.Key);

foreach (var d in duplicates)
{
Console.WriteLine(d);
}
Console.ReadLine();
}
}
}

Out put :

4
2
3
1
6

1 comment:

  1. I guess List.Distinct() would also do.

    ReplyDelete

MS CRM 2011 KB Article customization Issue.

Recently I have encountered some issue while customizing Kb Article Entity. I was doing following configuration in Article form. 1. Add Ba...