如何将Datetime外部传递给SQL

时间:2016-10-16 09:05:44

标签: linq

如何编写获取该记录的代码通过传递Date,MM,Year,HH,MM,SS 所有这些参数都来自DropdownList

public JsonResult Dif(int Day = 0, int Month=0, int Year=0, int HH, int MM, int Ss)
{
    var x = (from n in db.Employees
             where n.DataofBirth = (Day,Month,Year)
             select n).First();
    return new JsonResult { JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}

1 个答案:

答案 0 :(得分:2)

您需要创建DateTime的实例:

var date = new DateTime(Year, Month, Day, HH, MM, Ss);
var x = (from n in db.Employees
         where n.DataofBirth = date
         select n).First();

此外,参数具有默认值并不合理......特别是如果它不是有效值!年,月或日都不能为0。