C# DATEDIFF如何在Asp.net MVC中编写

C# DATEDIFF如何在Asp.net MVC中编写,c#,asp.net-mvc,C#,Asp.net Mvc,我正在创建一个汽车零售系统。如果我输入了正确的carid,我需要显示相关的车辆信息,这些信息将显示在下面的文本框中。我已附上下面的屏幕截图。我需要计算开始日期和结束日期之间的日差,以计算两者之间的天数,因为零售系统的额外天数需要计算零售费用。在C语言中,我这样写,选择car_id,cust_id,due,DATEDIFFGETDATE,due作为租车的elap,其中car_id=? 我不知道如何在Asp.NETMVC中编写 我试过的代码: 形式设计 我不知道如何写Datediff来计算天数。在这

我正在创建一个汽车零售系统。如果我输入了正确的carid,我需要显示相关的车辆信息,这些信息将显示在下面的文本框中。我已附上下面的屏幕截图。我需要计算开始日期和结束日期之间的日差,以计算两者之间的天数,因为零售系统的额外天数需要计算零售费用。在C语言中,我这样写,选择car_id,cust_id,due,DATEDIFFGETDATE,due作为租车的elap,其中car_id=? 我不知道如何在Asp.NETMVC中编写

我试过的代码:

形式设计

我不知道如何写Datediff来计算天数。在这种情况下,不显示结果。我测试了custid,它没有显示

数据库字段

您可以像这样使用SqlFunctions.DateDiff:

   var carn = (from s in db.rentails where s.carno == carno 
       select new {   
       StartDate = s.sdate,
       EndDate = s.edate,
       CarNo = s.carno,
       Fee = s.fee,
       ElapsedDays = SqlFunctions.DateDiff("day",DateTime.Now,s.sdate)
    }).ToArray();
.

您可以像这样使用SqlFunctions.DateDiff:

   var carn = (from s in db.rentails where s.carno == carno 
       select new {   
       StartDate = s.sdate,
       EndDate = s.edate,
       CarNo = s.carno,
       Fee = s.fee,
       ElapsedDays = SqlFunctions.DateDiff("day",DateTime.Now,s.sdate)
    }).ToArray();

在C语言中,您不需要像在SQL中那样编写Datediff函数。您只需减去所需日期:

var carn = (from s in db.rentails where s.carno == carno select s).
.ToList()
.Select(x => new 
{
DateDiff = (x.edate - x.sdate).Days,
//rest props
});

在C中,您不需要像在SQL中那样编写Datediff函数。您只需减去所需日期:

var carn = (from s in db.rentails where s.carno == carno select s).
.ToList()
.Select(x => new 
{
DateDiff = (x.edate - x.sdate).Days,
//rest props
});

请尝试以下代码:

var carn = (from s in db.rentails where s.carno == carno select s)
.Select(x => new 
{
  DateDiff = (x.edate - x.sdate).Days
});

注意:确保eDate的类型,sDate是DateTime。

请尝试以下代码:

var carn = (from s in db.rentails where s.carno == carno select s)
.Select(x => new 
{
  DateDiff = (x.edate - x.sdate).Days
});


注意:确保eDate的类型,sDate是DateTime。

var carn=from s in db.rentails其中s.carno==carno select s.custid.Selectt=>s.sDate{ElapsedDays=SqlFunctions.DateDiffday,DateTime.Now,t.eDate}.ToArray;错误displayed@Javafiver我忘了添加新关键字。你的语法不正确。请参阅db.rentails中的var carn=from s,其中s.carno==carno select s.custid.Selectt=>new{//这里如何选择开始日期ElapsedDays=SqlFunctions.DateDiffday,DateTime.Now,t.sdate}.ToArray;开始sdate enddate edate此数据库表colums@Javafiver在db.rentails中从s生成有效的select语句。var carn=其中s.carno==carno select s.custid.Selectt=>s.sdate{ElapsedDays=SqlFunctions.DateDiffday,DateTime.Now,t.edate}.ToArray;错误displayed@Javafiver我忘了添加新关键字。你的语法不正确。请参阅db.rentails中的var carn=from s,其中s.carno==carno select s.custid.Selectt=>new{//这里如何选择开始日期ElapsedDays=SqlFunctions.DateDiffday,DateTime.Now,t.sdate}.ToArray;开始sdate enddate edate此数据库表colums@Javafiver生成了有效的select语句。DateDiff=x.edate-x.sdate.Days,此行x.edate-x.sdate从db.rentails中的s获取错误sirvar carn=,其中s.carno==carno select s。此错误表示errorDateDiff=x.edate-x.sdate.Days,此行x.edate-x.sdate从db.rentails中的s获取错误sirvar carn=s,其中s.carno==carno select s。此错误表示此行中的errorDateDiff=x.edate-x.sdate.Days Days天错误可能您没有为此使用名称空间?因为它对我来说很好。Days这行中添加DateDiff=x.edate-x.sdate.Days的名称空间是什么?Days错误也许你没有使用名称空间?因为它对我来说很好。天,要添加什么名称空间
var carn = (from s in db.rentails where s.carno == carno select s)
.Select(x => new 
{
  DateDiff = (x.edate - x.sdate).Days
});