TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Converting Lists to Collections in C# and Using Linq on it
Bhushan Gawale
Jan 05, 2015
56.3
k
0
1
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
Overview of how you can convert .NET Lists to Collections.
All right guys, this one is one of the shortest blog post I am doing today and hope it helps people like me who wasted almost few minutes searching for solution on this.
Basically .NET Framework offers us too many collections however two of those are
List
Collection
We all know Linq, i.e. this nice and slick feature of .NET to query various collections however when it comes to Collections then we get this famous typecasting error
i..e Cannot convert IEnumerable to Collection etc etc..
Well, after several minutes of discussion with my brainy peers and some search on internet here is what I have done
Its a workaround which works in two steps
Convert your Collection to List and perform Linq on it.
Convert back your List to Collection. How? here we go
Collection<Entity> collection = new ObservableCollection<Entity>(collection.ToList().Distinct());
And you are done.
Hope this helps someone.
Next Recommended Reading
Convert C# Datatable To Excel File Using NPOI DLL C#