-2

这是我的看法

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;

            }

这是我的代码

它给

错误隐式转换

4

3 回答 3

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

于 2013-02-08T12:26:44.420 回答
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;
}
于 2013-02-08T12:22:41.423 回答
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
于 2013-02-08T12:41:30.083 回答