This is a follow up question to this question. You should read that first.
I have now, thanks to this answer, created a query that will return the correct entries. See:
IQueryable<Data> onePerHour = dataLastWeek
.Where(d =>
!dataLastWeek
.Any(d2 =>
d2.ArchiveTime.Date == d.ArchiveTime.Date &&
d2.ArchiveTime.Hour == d.ArchiveTime.Hour &&
d2.ArchiveTime < d.ArchiveTime));
Now for processing the entries and displaying them on a chart, I only need one or two properties of the model class Data
. The use case is something like this:
List<Data> actualData = onePerHour.ToList();
var tempCTupels = new List<TimeTupel<float>>();
tempCTupels.AddRange(actualData.Select(d => new TimeTupel<float>(d.ArchiveTime, d.TempC)));
var co2Tupels = new List<TimeTupel<float>>();
tempCTupels.AddRange(actualData.Select(d => new TimeTupel<float>(d.ArchiveTime, d.CO2Percent)));
TimeTupel
is very simple and defined like this:
public class TimeTupel<TData>
{
public TimeTupel(DateTime time, TData yValue)
{
Time = time;
YValue = yValue;
}
public DateTime Time { get; set; }
public TData YValue { get; set; }
}
Question
Currently actualdata
is a List<Data>
which means it's fully loaded in memory.
Since I only use two properties I wouldn't need to retrieve the whole object to create the TimeTupel
s.
Now my question is how would I achieve a performance increase? Is it the correct approach to remove the ToList
?
Things I've tried
Just using the
IQueryable<Data>
to create theTimeTupel
:
IQueryable<Data> actualData = onePerHour;
yields a runtime error ("System.InvalidOperationException: 'Null TypeMapping in Sql Tree'")Using
AsEnumerable
:
IEnumerable<Data> actualData = onePerHour.AsEnumerable();
is slow, takes around 22 seconds for 10 days worth of dataUsing
ToList
as seen in the above code (ToArray
is almost equal):
List<Data> actualData = onePerHour.ToList();
is faster, takes around 5 seconds for the same amount of data