I have db table Model.
Here is code:
public partial class Logging
{
public string Imei { get; set; }
public DateTime CurDateTime { get; set; }
public Nullable<System.DateTime> GPSDateTime2 { get; set; }
public Nullable<decimal> Latitude2 { get; set; }
public Nullable<decimal> Longitude2 { get; set; }
public int Speed { get; set; }
public Nullable<int> Datatype { get; set; }
public int Id { get; set; }
}
I want to calculate a difference
And decided to create View - Model
Here is code:
public class HeatmapViewModel
{
public int FirstStartDifference { get; set; }
public int LastStartDifference { get; set; }
public int coeff = 2;
public int Difference;
}
And I decided to make calculations on Repository layer Here is code:
public List<HeatmapViewModel> GetStops()
{
using (var ctx = new GoogleMapTutorialEntities())
{
List<HeatmapViewModel> items = new List<HeatmapViewModel>();
#region firstitem_calculation
var firstitem = ctx.Loggings.Where(x => x.Datatype == 2).AsEnumerable().Select(
x => new Logging
{
Longitude2 = x.Longitude2,
Latitude2 = x.Latitude2,
CurDateTime = x.CurDateTime
}).FirstOrDefault();
var midnight = new DateTime(firstitem.CurDateTime.Year, firstitem.CurDateTime.Month, firstitem.CurDateTime.Day, 00, 00, 00);
TimeSpan difference = (firstitem.CurDateTime - midnight);
var difference_after_midnight = (int) difference.TotalMinutes;
items.Add( new HeatmapViewModel
{
FirstStartDifference = difference_after_midnight
});
#endregion
#region lastitem_calculation
var lastItem = ctx.Loggings.Where(x => x.Datatype == 2).AsEnumerable().Select(
x => new Logging
{
Longitude2 = x.Longitude2,
Latitude2 = x.Latitude2,
CurDateTime = x.CurDateTime
}).LastOrDefault();
var before_midnight = new DateTime(lastItem.CurDateTime.Year, lastItem.CurDateTime.Month, lastItem.CurDateTime.Day, 23, 59, 00);
TimeSpan difference_before = (before_midnight - lastItem.CurDateTime);
var difference_before_midnight = (int)difference_before.TotalMinutes;
items.Add(new HeatmapViewModel
{
LastStartDifference = difference_after_midnight
});
#endregion
return items;
}
}
And call on Controller just method from repo.
Like this:
public JsonResult GetStops()
{
var stops = repo.GetStops();
return Json(stops.ToArray(), JsonRequestBehavior.AllowGet);
}
I want to know if the code on the repository is okay, or can I improve it?
Nullable<>
right? \$\endgroup\$