Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Converting Lists to Collections in C# and Using Linq on it
WhatsApp
Bhushan Gawale
10y
57.6
k
0
1
25
Blog
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.
Up Next
Convert C# Datatable To Excel File Using NPOI DLL C#
Ebook Download
View all
LINQ Quick Reference with C#
Read by 44.7k people
Download Now!
Learn
View all
Membership not found