【发布时间】:2018-04-04 21:20:07
【问题描述】:
我有一些列的表。这是表类
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 string Speed { get; set; }
public Nullable<int> Datatype { get; set; }
public int Id { get; set; }
我需要在 Datatype = 2 的表中找到最后一条记录,并找到差异 23:59 - CurDateTime。
为了区别我写了属性
这里是代码
[NotMapped]
public TimeSpan? LastStartDifference
{
get
{
if (CurDateTime != null)
{
var midnight = new DateTime(CurDateTime.Year, CurDateTime.Month, CurDateTime.Day, 23, 59, 00);
var difference = midnight - CurDateTime;
return difference;
}
return null;
}
}
对于第一个元素,我编写了这样的代码
public JsonResult GetStops()
{
using (var ctx = new GoogleMapTutorialEntities())
{
var firstitem = ctx.Loggings.Where(x => x.Datatype == 1).AsEnumerable().Select(
x => new
{
lng = x.Longitude2,
lat = x.Latitude2,
difference = (int)(x.FirstStartDifference?.TotalMinutes ?? -1) * x.coeff
}).FirstOrDefault();
return Json(firstitem, JsonRequestBehavior.AllowGet);
}
}
但是我怎样才能找到Datatype == 2 的最后一个元素呢?
【问题讨论】:
标签: c# mysql asp.net asp.net-mvc entity-framework