Different between ToList and ToArray in Entity Framework

wkm1925
 
on Jul 08, 2021 11:27 PM
421 Views

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

I've result set as following,

Raw View

https://i.ibb.co/X8PSqCc/09072021-001.png

Into Array

https://i.ibb.co/gW4kkPx/09072021-002.png

How to convert List Raw View into List of Array?

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
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.

https://stackoverflow.com/questions/1105990/is-it-better-to-call-tolist-or-toarray-in-linq-queries

https://dotnettutorials.net/lesson/tolist-and-toarray-methods-linq/