Entity Framework: ToList, ToArray, ToDictionary, ToLookup, GroupBy

When I want to bring several records from a database, personally, I tend to use the ToList method almost exclusively. Certainly, this is not the only way to bring a collection of records from a database. We have other options, like ToDictionary, which we could consider depending on the situation in which we find ourselves. In this post, we will talk about the methods: ToList, ToArray, ToDictionary, ToLookup and GroupBy.

We will work with Entity Framework Core 2.0 in this post.

ToList

The ToList method allows us to obtain a list of elements of a database using Entity Framework. We could say that it is the default method that people use to bring collections of records. This is due to the powerful amount of methods that lists offer us.

ToArray

Very similar to the lists, arrays allows us to have simple collections of objects. We tend to prefer lists because arrays have fewer methods which we can take advantage of. One of the features offered by the lists that the arrays do not, is the ability to add items to the list without difficulties, whereas an array would have to be resized. An occasion in which we can perhaps use ToArray, is if we will consume a method that requires a parameter of type array.

ToDictionary

Dictionaries are structures which associate a key to a value. If you have the need to keep a set of records in memory, and you want to access certain records in a quick way, like using its Id, then the ToDictionary is a good option.

ToLookup

Convert the records into an ILookup. It is useful for making quick searches in memory of the records that were brought. In addition, it is used to group records with common characteristics. ILookup create a one-to-many relationship between a key and a set of values. An example would be an ILookup where the key is the student’s gender, and the values ​​are the students of that gender.

GroupBy

Convert the records into an IGrouping. It does everything ILookup does, with the difference that the ToLookup runs immediately, while the GroupBy’s execution is deferred.

Example

using (var context = new ApplicationDbContext ())

{

var studentsList = context.Students.ToList ();

var studentsArray = context.Students.ToArray ();

var studentsDictionary = context.Students.ToDictionary (x => x.Id);

var studentsLookup = context.Students.ToLookup (x => x.Gender);

var studentsGrouped = context.Students.GroupBy (x => x.Gender);

}

As you can see, the first two examples, ToList and ToArray have no parameters or complications. In the case of ToDictionary what we are doing is to indicate that the dictionary key, which will be the value of the Id field, which is a unique field.

In the case of ToLookup, we are grouping by a field called Gender, which contains the student’s gender. The lookup is like a dictionary, which associates a key with a collection of values, in our case, each gender is associated with a collection of students which belongs to that gender. The GroupBy does the same as the ToLookup, however, the GroupBy query will be executed in the future, because it is deferred. In fact, if you run the previous code, and you have logging capabilities activated in Entity Framework, you’d see only 4 queries, again, because GroupBy is deferred.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s