I have table with some columns . Here Is class for table
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; }
I need to find last record in table where Datatype = 2 and for it find difference 23:59 - CurDateTime.
For difference I wrote property
Here is code
[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;
}
}
For first element I wrote code like this
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);
}
}
But how I can find last element where Datatype == 2
?
you can use this: Use LastOrDefault and select Items with Datatype == 2
public JsonResult GetStops()
{
using (var ctx = new GoogleMapTutorialEntities())
{
var lastItem = ctx.Loggings.LastOrDefault(x => x.Datatype == 2).AsEnumerable().Select(
x => new
{
lng = x.Longitude2,
lat = x.Latitude2,
difference = (int)(x.LastStartDifference?.TotalMinutes ?? -1) * x.coeff
});
return Json(lastItem, JsonRequestBehavior.AllowGet);
}
}
我只是从“员工薪资”表中获取“净薪金”的最后一个增量金额:
var netsalary = UowObj.EmployeeSalaryRepository.GetAllData().Where(x => x.EmployeeID == data.ID && x.IsArchive==true).Select(x => x.NetSalary).LastOrDefault();
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.