Different between ToList and ToArray in Entity Framework

on Jul 08, 2021 11:27 PM

What is the different between List into Raw View and List into Array

I've result set as following,

Raw View


Into Array


How to convert List Raw View into List of Array?

Download FREE API for Word, Excel and PDF in ASP.Net: Download
on Jul 09, 2021 12:00 AM

Hi wkm1925,

ToList calls List<T>(IEnumerable<T>) constructor to create a List<T>, while ToArrary uses an internal class Buffer<T> to grow the array.

If the source collection (IEnumerable<T>) implements the ICollection interface, the two methods use similar code logic to copy the data.

Otherwise, ToList will creates the List<T> dynamically.

While ToArray copies the element one by one into a new array. If the array if full, the method doubles the array size to hold the data.

Finally the method returns another array based on the source collection's size.

Unless you simply need an array to meet other constraints you should use ToList. In the majority of scenarios ToArray will allocate more memory than ToList.

Both use arrays for storage, but ToList has a more flexible constraint. It needs the array to be at least as large as the number of elements in the collection. If the array is larger, that is not a problem. However ToArray needs the array to be sized exactly to the number of elements.

Refer below link for more details.