I’ve been getting stuck into some linq queries for the first time today and I’m struggling with some of the more complicated ones. I’m building a query to extract data from a table to build a graph. The tables colums I’m interested in are Id, Time and Value.
The user will select a start time, an end time and the number of intervals (points) to graph. The value column will averaged for each interval.
I can do this with a linq request for each interval but I’m trying to write it in one query so I only need to go to the database once.
So far I have got:
var timeSpan = endTime.Subtract(startTime);
var intervalInSeconds = timeSpan.TotalSeconds / intervals;
var wattList = (from t in _table
where t.Id == id
&& t.Time >= startTime
&& t.Time <= endTime
group t by intervalInSeconds // This is the bit I'm struggling with
into g
orderby g.Key
select g.Average(a => a.Value))
).ToList();
Any help on grouping over time ranges will be most welcome.
I’ve done this myself for exactly the same situation you describe.
For speed, modified the database’s datapoints table to include an integer-based time column,
SecondsSince2000, and then worked with that value in my LINQ to SQL query.SecondsSince2000is a computed column defined as:Where
DataPointTimeColumnis the name of the column that stores the datapoint’s time. The magic function calldateadd(month,1200,0)returns 2000-01-01 at midnight, so the column stores the number of seconds since that time.The LINQ to SQL query is then made much simpler, and faster:
If you can’t modify your database, you can still do this:
The query will be quite a bit slower.