这是我的观点
SELECT AmenityValidDatesPriceID, AmenityID, AmenityValidDatesFrom, AmenityValidDatesTo, AddedDate, LastModifyDate, AddedUser, LastModifyUser,
IsDeleted
FROM dbo.DataManagement_Hotels_Amenities_AmenityValidDatesPrice
WHERE (IsDeleted = 0)
它返回AmenityValidDatesFrom,AmenityValidDatesTo作为日期时间
在前端我只需要日期
public List<BLL.Entities.DataManagement_Hotels_Amenities_AmenityValidDatesPrice> SelectByAmenityID(int ID)
{
DAL.DynamicSearchViewDataContext Viewcontext = new DAL.DynamicSearchViewDataContext();
DAL.SelectSectionDataContext SpContext = new DAL.SelectSectionDataContext();
var Query = (from car in Viewcontext.View_DataManagement_Hotel_Amenities_Dates
where car.AmenityID.Equals(ID)
select new BLL.Entities.DataManagement_Hotels_Amenities_AmenityValidDatesPrice
{
AmenityID = (int)car.AmenityID,
//AmenityValidDatesFrom = (DateTime)car.AmenityValidDatesFrom,
//AmenityValidDatesTo = (DateTime)car.AmenityValidDatesTo
AmenityValidDatesFrom = ((DateTime)car.AmenityValidDatesFrom).Date,
AmenityValidDatesTo = ((DateTime)car.AmenityValidDatesTo).Date
}).ToList<BLL.Entities.DataManagement_Hotels_Amenities_AmenityValidDatesPrice>();
return Query;
}
这是我的代码
它给予
错误隐式转换
答案 0 :(得分:1)
请改变您的观点,如下所示:
SELECT AmenityValidDatesPriceID,
AmenityID,
CONVERT(VARCHAR, AmenityValidDatesFrom,101) AS AmenityValidDatesFrom,
CONVERT(VARCHAR, AmenityValidDatesTo,101) AS AmenityValidDatesTo,
CONVERT(VARCHAR, AddedDate,101) AS AddedDate,
CONVERT(VARCHAR, LastModifyDate,101) AS LastModifyDate,
AddedUser,
LastModifyUser,
IsDeleted
FROM dbo.DataManagement_Hotels_Amenities_AmenityValidDatesPrice
WHERE IsDeleted = 0
它应该有用。
更新
需要进行空检查。
public List<BLL.Entities.DataManagement_Hotels_Amenities_AmenityValidDatesPrice> SelectByAmenityID(int ID)
{
DAL.DynamicSearchViewDataContext Viewcontext = new DAL.DynamicSearchViewDataContext();
DAL.SelectSectionDataContext SpContext = new DAL.SelectSectionDataContext();
var Query = (from car in Viewcontext.View_DataManagement_Hotel_Amenities_Dates
where car.AmenityID.Equals(ID)
select new BLL.Entities.DataManagement_Hotels_Amenities_AmenityValidDatesPrice
{
AmenityID = (int)car.AmenityID,
if (car.AmenityValidDatesFrom != null && car.AmenityValidDatesFrom.ToString() != "")
{
AmenityValidDatesFrom = car.AmenityValidDatesFrom.Date
},
if (car.AmenityValidDatesTo != null && car.AmenityValidDatesTo.ToString() != "")
{
AmenityValidDatesTo = car.AmenityValidDatesTo.Date
}
}).ToList<BLL.Entities.DataManagement_Hotels_Amenities_AmenityValidDatesPrice>();
return Query;
}
答案 1 :(得分:1)
试试这个
SELECT AmenityValidDatesPriceID ,
AmenityID ,
CONVERT(VARCHAR(10), AmenityValidDatesFrom, 103) AS AmenityValidDatesFrom ,
CONVERT(VARCHAR(10), AmenityValidDatesTo, 103) AS AmenityValidDatesTo ,
CONVERT(VARCHAR(10), AddedDate, 103) AS AddedDate ,
CONVERT(VARCHAR(10), LastModifyDate, 103) AS LastModifyDate ,
AddedUser ,
LastModifyUser ,
IsDeleted
FROM dbo.DataManagement_Hotels_Amenities_AmenityValidDatesPrice
WHERE IsDeleted = 0
它将以'dd / mm / yyyy'格式给出日期。如果您想以“mm / dd / yyyy”格式获取日期,请使用101代替103
答案 2 :(得分:0)
使用此视图来保留DATETIME
数据类型,同时删除时间部分:
SELECT AmenityValidDatesPriceID ,
AmenityID ,
DATEADD(D, 0, DATEDIFF(D, 0, AmenityValidDatesFrom)) AS AmenityValidDatesFrom
DATEADD(D, 0, DATEDIFF(D, 0, AmenityValidDatesTo)) AS AmenityValidDatesTo
DATEADD(D, 0, DATEDIFF(D, 0, AddedDate)) AS AddedDate
DATEADD(D, 0, DATEDIFF(D, 0, LastModifyDate)) AS LastModifyDate
AddedUser ,
LastModifyUser ,
IsDeleted
FROM dbo.DataManagement_Hotels_Amenities_AmenityValidDatesPrice
WHERE IsDeleted = 0