如何在LINQ查询中仅显示datetime字段的datepart?
我在数据库中有一个datetime字段,检索该字段时只显示日期,不显示时间。你能告诉我怎么做吗?以下是我写的问题:如何在LINQ查询中仅显示datetime字段的datepart?,linq,linq-to-sql,datetime,Linq,Linq To Sql,Datetime,我在数据库中有一个datetime字段,检索该字段时只显示日期,不显示时间。你能告诉我怎么做吗?以下是我写的问题: var queryProductEventCustomers = (from r in DbContext.ProductEventCustomers from s in DbContext.CustomerProducts
var queryProductEventCustomers = (from r in DbContext.ProductEventCustomers
from s in DbContext.CustomerProducts
where r.Customers_Id == customerID && r.Customers_Id
== s.Customers_Id && s.Products_Id == productID
select new
{
r.Id,
r.Customers_Id,
r.StartTime
starttime是一个日期时间字段。那么,你能告诉我怎么做吗?只需选择日期部分:
select new
{
Id = r.Id,
Cust_id = r.Customers_Id,
Date = r.StartTime.Date
}
试试这个
select new
{
ID = r.Id,
CustomerID = r.Customers_Id,
StartDate = r.StartTime.ToString("dd/MM/yyyy")
}
使用短日期格式:
r.StartTime.ToString("d", DateTimeFormatInfo.InvariantInfo)
该链接向您展示了如何使用所有标准日期和时间格式。在DateTime上使用该方法
select new
{
Id = r.Id,
Customers_Id = r.Customers_Id,
Date = r.StartTime.ToShortDateString()
}
r.starttimestat无法编译后,不会出现日期属性。您需要为date.Nishanth指定一个成员-StartTime的数据类型是什么?DateTime应该有一个“date”属性,因此StartTime.date应该可以工作。您可能应该在代码中验证StartTime实际上是一个日期时间,而不是不会编译的其他类型的字段(例如字符串等)。您需要为日期指定一个成员。@nishanth,您能让它工作吗?