简体   繁体   English

获取表中的特定数据的最后一个条目(ASP.NET MVC)

[英]Get Last entry with specific data in table(ASP.NET MVC)

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. 我需要在数据类型= 2的表中找到最后一条记录,并找到差异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 ? 但是,如何找到Datatype == 2最后一个元素?

you can use this: Use LastOrDefault and select Items with Datatype == 2 您可以使用此方法:使用LastOrDefault并选择数据类型== 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM