LINQ 'AsEnumerable' and Entity Framework

I am writing a LINQ query similar to the following:

var test = from o in dbcontext.Orders.AsEnumerable() where o.ID==1 select new Order { Name = GetName(o.ID) }; 

To call an external function in a LINQ query, I use AsEnumerable() in the query.

I understand that usually a request is not executed until an enumeration function of the ToList() type is ToList() , etc. But here I call the listing in the request.

Can someone tell me what is considered improper practice to use AsEnumerable() like this? Will it ToList() performance compared to calling ToList() after creating the request?

+4
source share
1 answer

I would do

 var ids = from o in dbcontext.Orders where o.ID==1 select new { ID = o.ID }; var names = from i in ids.AsEnumerable() select new Order { Name = GetName(i.ID) }; 

i.e. make as many queries in the database as possible, and then just do the ID-to-name conversion to C #.

+5
source

All Articles