0

I have following collection:

public IList<Emp> EmpList()
{
    IList<Emp> empList = new List<Emp>();
    empList.Add(new Emp() { Id = 1, Name = "abc1" });
    empList.Add(new Emp() { Id = 1, Name = "abc2" });        
    empList.Add(new Emp() { Id = 2, Name = "abc3" });
    empList.Add(new Emp() { Id = 2, Name = "abc4" });
    empList.Add(new Emp() { Id = 4, Name = "abc5" });
    return empList;
}

I wants to get distinct records based on Id from the EmpList() collection. I mean first row, third row and last row from the EmpList() collection.

Any help would be greatly appreciated.

Edit-var empList = emp.EmpList().GroupBy(x => x.Id).Select(x => x.First()); This query is working fine for the solution.

mnmnmnmnm
  • 229
  • 1
  • 2
  • 8

2 Answers2

4

You can use this, from MoreLINQ

public static IEnumerable<TSource> DistinctBy<TSource, TKey>
    (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
    HashSet<TKey> seenKeys = new HashSet<TKey>();
    foreach (TSource element in source)
    {
        if (seenKeys.Add(keySelector(element)))
        {
            yield return element;
        }
    }
}

and use it like:

return empList.DistinctBy(x => x.Id).ToList();
Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
  • Is there any alternate solution because I don't want to use MoreLinq. – mnmnmnmnm Dec 05 '13 at 10:31
  • 1
    @mnmnmnmnm you can paste this code as extension method and use it like I suggested. To use extension method you need to have static class: EnumerableExtensions and put there this method – Kamil Budziewski Dec 05 '13 at 10:31
  • !wudzik, Yes I got it but here i have pasted sample but in real scenario I have to implement it in the Vb.Net, and I guess Vb.net does not support extension method :( – mnmnmnmnm Dec 05 '13 at 10:34
  • 3
    @mnmnmnmnm Huh? Of course VB.Net supports extension methods. – sloth Dec 05 '13 at 10:35
  • @mnmnmnmnm http://msdn.microsoft.com/en-us/library/bb384936.aspx look here for extension methods in vb – Kamil Budziewski Dec 05 '13 at 10:35
  • @wudzik,, thanks for letting me know..yes i tried and found that vb.net also supports extension method. Thanks :) – mnmnmnmnm Dec 05 '13 at 10:39
  • But meanwhile I have written var empList = emp.EmpList().GroupBy(x => x.Id).Select(x => x.First()); and now its also working fine :) – mnmnmnmnm Dec 05 '13 at 10:39
  • @mnmnmnmnm of course you can use `groupby` to do this :) but personally I prefer `DistinctBy`, your choice :) – Kamil Budziewski Dec 05 '13 at 10:40
  • +1 -i literally picked this little extension method up a few weeks back and use it with such joy now. my varsion is slightly different and uses linq, rather than loops but is functionally identical. – jim tollan Dec 05 '13 at 10:41
  • This is kind of a lazy alternative to writing an equality comparer and using the [provided overload of `Distinct`](http://msdn.microsoft.com/en-us/library/bb338049.aspx), with the bonus side effect that you *know* this will always return the *first* item per key, while `Distinct` makes no such guarantee. – Rawling Dec 05 '13 at 10:52
4
var q = from e in empList
        group e by e.Id into grps
        select grps.First();
Alireza
  • 10,237
  • 6
  • 43
  • 59