I have a simple entity
public class MyEntity
{
public DateTime Date { get; set; }
public string Code { get; set; }
public decimal Value { get; set; }
}
I store it with EF to a SQL database and all is fine. A sample of the data is shown here. A certain Code can have data for a given date but not for other dates.
| Date | Code | Value |
+------------+--------+--------+
| 2016-10-01 | AA | .5 |
| 2016-10-02 | AA | .6 |
| 2016-10-03 | AA | .7 |
| 2016-09-30 | BB | .51 |
| 2016-10-02 | BB | .64 |
| 2016-10-04 | BB | .75 |
| 2016-09-30 | CC | .51 |
Now I am wondering, what is the best way to retrieve the data so that I can have a result that include all dates in a given range? So that it looks like this:
| 2016-10-02 | AA | .6 |
| 2016-10-03 | AA | .7 |
| 2016-10-04 | AA | .7 | <<-- this is same as day before
| 2016-10-02 | BB | .64 |
| 2016-10-03 | BB | .64 | <<-- this is same as day before
| 2016-10-04 | BB | .75 |
| 2016-10-02 | CC | .51 | <<-- this is latest available value
| 2016-10-03 | CC | .51 |
| 2016-10-04 | CC | .51 |
Basically the rule is that if for a day there is missing data, then I take the first day before that date.
The query requests data in a given date range and let's assume there is always at least an entity for a previous date in the database so whatever the query, I can always have a value for a latest available value.
I can get this information by means of several LINQ queries and some code but i wonder if there is a good approach I can take or even a SQL syntax that can help?
So far I am looping the dates in my range using the following query and method
public void CreateReportForDates()
{
var dates = new [] { DateTime.Today, DateTime.Today.AddDay(-1) } ;
var values = this.GetDataByDateRange(dates);
.... do something with these values ....
}
public ICollection<MyEntity> GetDataByDate(EntityContext dbContext, DateTime date)
{
var queryInDay = from element in dbContext.MyEntities
where element.Date <= date
group element by element.Code
into groupedElements
select groupedElements.OrderBy(x => x.Date).First();
return queryInDay.ToList();
}
public IEnumerable<MyEntity> GetDataByDateRange(IEnumerable<DateTime> dates)
{
foreach(var date in dates)
{
var dbContext = new EntityContext();
foreach(var result in this.GetDataByDate(dbContext, date)
{
result.Date = date;
yield return result;
}
dbContext.Dispose();
}
}
I can't say if it's possible with pure LINQ to Entities query, but even it is, it would be quite inefficient.
I would still use a mixed approach by getting only the necessary data from the database using a single query and expand the result in memory. For the expansion I would order the records by {Code, Date}
(inside the database) and then use quite efficient iterative (non LINQ) approach:
public IEnumerable<MyEntity> GetDataByDateRange(DateTime startDate, DateTime endDate)
{
var dbContext = new EntityContext();
var dbQuery =
from g in (from e in dbContext.MyEntities
where e.Date <= startDate
group e.Date by e.Code into g
select new { Code = g.Key, MaxDate = g.Max() })
join e in dbContext.MyEntities on g.Code equals e.Code
where e.Date >= g.MaxDate && e.Date <= endDate
orderby e.Code, e.Date
select e;
using (var result = dbQuery.GetEnumerator())
{
for (bool more = result.MoveNext(); more;)
{
MyEntity next = result.Current, item = null;
for (var date = startDate; date <= endDate; date = date.AddDays(1))
{
if (item == null || (next != null && next.Date == date))
{
item = next;
more = result.MoveNext();
next = more && result.Current.Code == item.Code ? result.Current : null;
}
else if (item.Date != date)
item = new MyEntity { Date = date, Code = item.Code, Value = item.Value };
yield return item;
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.