It’s a simple question but i don’t really know how to do it efficiently. Is there any way to efficiently convert list of values to array e.g List<int> to int[] OR List<CustomObj> to CustomObj[] directly without using loops preferably by using Linq?
In addition, I have a GenericCollection<T>, how can i convert the Linq query to GenericCollection<T> directly without looping e.g.
GenericCollection<T> result = SomeGenericCollection.Select(o => o).ToList<GenericCollection<T>>();
for the first part of your question, look at the
ToArray()method as per the other answers:List<T>.ToArray(): http://msdn.microsoft.com/en-us/library/x303t819.aspxEnumerable.ToArray<T>()extension method: http://msdn.microsoft.com/en-us/library/x303t819.aspx#Y0For the second part, you can write your own extension method:
Once you have this, you can convert any collection to your
GenericCollection<T>as follows:You really can’t avoid having to write the loop, but this way you only have to write it once.