[C#] Cast Collection to List with Linq

Sometimes it is better to work with a List than a collection, but how to cast if only a collection is available?
And to keep it short, how to accomplish that with Linq?

The answer is simple:

var resultList = (from TYPE mItem in originCollection select mItem).ToList();

Explained:

  • TYPE is the Type of Items in the Collection.
  • mItem is an anonymous variable used to declare a single item
  • resultList is a List of type TYPE containing all items from the collection

I will not explain the basics of Lists and Collections to keep the post short.

Robin

Software Engineer / Application Developer

Subscribe to robsCode

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!