簡體   English   中英

獲取表中的特定數據的最后一個條目(ASP.NET MVC)

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

我的桌子上有幾列。 這是桌子的課

 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; }

我需要在數據類型= 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最后一個元素?

您可以使用此方法:使用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