Good day!
I have a client in C # and SQLServer 2012. I use EF6 to connect.
I write something like this:
using (PCMSContext context = new PCMSContext()) { Dictionary<Guid, pidPV> pPVs = context.pidPV.GroupBy(e => e.idPID).Select(g => g.OrderByDescending(f => f.date).FirstOrDefault()).ToDictionary(e => e.idPID); } I care about the minimum load on the network and the client, respectively, I have a question of the following plan:
Up to what point is this LINQ query translated to SQL database query?
It is important that the operation g => g.OrderByDescending(f => f.date).FirstOrDefault() performed on the database side, since pidPV just a huge number of pidPV rows in the table and I don’t need to load them all onto the client and do the sorting and fetching of the first element locally on the client.
I know that ToDictionary() in any case executed on the client side, because this extension method works with a collection that implements IEnumerable.
Is g => g.OrderByDescending(f => f.date).FirstOrDefault() on the SQLServer side in this case?
ToDictionaryexecuted on the server. Before executing the query, you can hang a logger on the Log property and see which sql is generated. The easiest:context.Database.Log += Console.WriteLine;- Alexander PetrovTOP (1). And already on the client, ifNULLcame from the database, in the case ofFirstexception will be thrown, and in the case ofFirstOrDefaultwill benull. - Alexander Petrov