Reputation: 1776
I am investigating slow performance over the network of my application which uses EntityFramework version 5. One option I tried was to retrieve only the Id field, instead of the entire object. However, examining in Wireshark, I see that all objects are transferred anyway. In other words, the following two code blocks cause exactly the same network activity. Would anyone know how I can cause the db to only return Ids in the first query?
List<long> test = dbContext.UserActivities
.Where(ua => ua.Activity.Id == activityId)
.Select(ua => ua.User)
.ToList()
.Select(u => u.Id)
.ToList();
List<long> test = dbContext.UserActivities
.Where(ua => ua.Activity.Id == activityId)
.Select(ua => ua.User)
.ToList();
Upvotes: 1
Views: 66
Reputation: 4607
.ToList() materializes the object. Essentially, executing the query. Anything after is LINQ to Objects.
try something like this:
List<long> test = dbContext.UserActivities
.Where(ua => ua.Activity.Id == activityId)
.Select(ua => ua.User.Id).ToList();
Upvotes: 4
Reputation: 26654
List<long> test = dbContext.UserActivities
.Where(ua => ua.Activity.Id == activityId)
.Select(ua => ua.User.Id).ToList();
Upvotes: 2