Problem: Filtering out(removing duplicates) from an IEnumerable

One possible solution with using linq would be to group by key and take the first element of each group. Here’s an example:

enumerable.GroupBy(x => x.key).Select(g => g.First())
Advertisements

Posted by TheBoyan

I have been programming since I was 11 years old (Yes, I'm one of those, for real). First programming language I learned was BASIC, on a Commodore64. Then around high-school it was C and Assembly. Just before and during university days it was C++. I started doing some professional work during this period, part of the reason why I dropped out of uni in my final year. I have never stopped since then. Going through a plethora of technologies ranging from C and Assembly languages, Delphi to C++, C#, Java and so on and so forth... trough 20 (and more, who counts, does it even matter) years of software development. I have not lost even a single bit of my ambition and love for the craft from those first young days, nor the energy...I absolutely love what I do. I like to get my hands into all aspects of software development. Now-a-days I use mainly Microsoft related technologies .NET C#, with a lot of database design/management usage of SQL server, but not in any way limited to that.

4 Comments

  1. Try Distinct(). You’ll like it.

    Like

    Reply

  2. Yeah, but there is no Distinct() for IEnumerable, unless you implement it using an extension method or something.

    Like

    Reply

    1. Use can easily cast to IEnumerable.

      collection.Cast().Distinct()

      Like

      Reply

      1. That is… Cast to IEnumerable of object… HTML got the better of me. 🙂

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s