Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework - Linq query with order by and group by

I have Measurement Objects with the relevant Properties CreationTime (DateTime) and Reference (String) and some other values.

I'd like to write an efficient linq query to a DbContext that

  • groups my Measurement objects by a given Reference
  • orders the groups by either the CreationTime Property of the first Measurement from the group or the average of the CreationTime properties
  • limits the query to the most recent numOfEntries

(In a later step i calculate averaged values over these returned groups, but i guess that's not relevant for my problem.)

The DbContext itself has a DbSet<Measurement> called Measurementsholding all Measurement objects.

I came up with the following query, that results in a List of groups that is ordered correctly but is missing some groups in between.

var groupByReference = (from m in context.Measurements
                          orderby m.CreationTime
                          group m by new { m.Reference } into g
                          select g).Take(numOfEntries).ToList();

How do I select the "most recent" groups of Measurements correctly?

I'm using Entity Framework 4.4 with a MySQL Database (MySql Connector/Net 6.6.4). This example is simplified, i can go into more detail and/or give an example if necessary.

Thanks!

like image 943
zufallsfund Avatar asked Mar 22 '13 13:03

zufallsfund


3 Answers

It's method syntax (which I find easier to read) but this might do it

Updated post comment

Use .FirstOrDefault() instead of .First()

With regard to the dates average, you may have to drop that ordering for the moment as I am unable to get to an IDE at the moment

var groupByReference = context.Measurements
                              .GroupBy(m => m.Reference)
                              .Select(g => new {Creation = g.FirstOrDefault().CreationTime, 
//                                              Avg = g.Average(m => m.CreationTime.Ticks),
                                                Items = g })
                              .OrderBy(x => x.Creation)
//                            .ThenBy(x => x.Avg)
                              .Take(numOfEntries)
                              .ToList();
like image 157
NinjaNye Avatar answered Nov 12 '22 14:11

NinjaNye


You can try to cast the result of GroupBy and Take into an Enumerable first then process the rest (building on the solution provided by NinjaNye

var groupByReference = (from m in context.Measurements
                              .GroupBy(m => m.Reference)
                              .Take(numOfEntries).AsEnumerable()
                               .Select(g => new {Creation = g.FirstOrDefault().CreationTime, 
                                             Avg = g.Average(m => m.CreationTime.Ticks),
                                                Items = g })
                              .OrderBy(x => x.Creation)
                              .ThenBy(x => x.Avg)
                              .ToList() select m);

Your sql query would look similar (depending on your input) this

SELECT TOP (3) [t1].[Reference] AS [Key]
FROM (
    SELECT [t0].[Reference]
    FROM [Measurements] AS [t0]
    GROUP BY [t0].[Reference]
    ) AS [t1]
GO

-- Region Parameters
DECLARE @x1 NVarChar(1000) = 'Ref1'
-- EndRegion
SELECT [t0].[CreationTime], [t0].[Id], [t0].[Reference]
FROM [Measurements] AS [t0]
WHERE @x1 = [t0].[Reference]
GO

-- Region Parameters
DECLARE @x1 NVarChar(1000) = 'Ref2'
-- EndRegion
SELECT [t0].[CreationTime], [t0].[Id], [t0].[Reference]
FROM [Measurements] AS [t0]
WHERE @x1 = [t0].[Reference]
like image 44
user3373870 Avatar answered Nov 12 '22 14:11

user3373870


Try moving the order by after group by:

var groupByReference = (from m in context.Measurements
                        group m by new { m.Reference } into g
                        order by g.Avg(i => i.CreationTime)
                        select g).Take(numOfEntries).ToList();
like image 1
MarcinJuraszek Avatar answered Nov 12 '22 15:11

MarcinJuraszek