Linq中的Convert.ToDateTime

时间:2013-07-08 12:33:58

标签: c# linq

我想生成类似这种SQL语句的LINQ语句

SELECT *
FROM   dbo.tbl_Advertisement
WHERE  FileName LIKE '%latest%'
       AND ToDate = (SELECT min(ToDate)
                     FROM   dbo.tbl_Advertisement
                     WHERE  CAST (getdate() AS DATE) <= CAST (Todate AS DATE)
                            AND FromDate = (SELECT max(FromDate)
                                            FROM   dbo.tbl_Advertisement
                                            WHERE  CAST (getdate() AS DATE) >= CAST (FromDate AS DATE)));

我已经生成了Linq语句如下:

objAdvList = objAdvList
            .Where(x => x.ToDate == Convert.ToDateTime(objAdvList
                .Where(y => y.FromDate == Convert.ToDateTime(objAdvList.Max(z => z.FromDate)))
                .Select(y => y.FromDate)
                ))
                .Select(x => x)
                .ToList();

其中objAdvList是

的List类型集合
class AdvertisementAccess
{
    public DateTime FromDate { get; set; }
    public DateTime ToDate { get; set; }
    public string FileName { get; set; }
    public string Path { get; set; }
}

但是这段代码给我一条错误信息如下:

  

无法投射类型的对象   'WhereSelectListIterator`2 [AdvertisementAccess,System.DateTime]'来   输入'System.IConvertible'。

有没有人可以解决我的问题?

此致 Mayank

5 个答案:

答案 0 :(得分:4)

您的内部查询会产生一组结果。但是,Convert.ToDateTime正在寻找单个值。您可以使用SingleFirstLast从内部查询生成的这组值中选择一个值。

要使用Single检索内部LINQ查询的结果,请尝试以下代码:

objAdvList = objAdvList
    .Where(x => x.ToDate == objAdvList
        .Where(y => y.FromDate == Convert.ToDateTime(objAdvList.Max(z => z.FromDate)))
        .Select(y => y.FromDate).Single()
    )
    .ToList();

或者,如果您需要从可能性列表中选择第一个结果,请改为使用First

objAdvList = objAdvList
    .Where(x => x.ToDate == objAdvList
        .Where(y => y.FromDate == Convert.ToDateTime(objAdvList.Max(z => z.FromDate)))
        .Select(y => y.FromDate).First()
    )
    .ToList();

在任何一种情况下,我都删除了.Select(x => x)表达式,它没有做任何事情。该表达式仅选择所有值x并返回它们未转换。

我还删除了对Convert.ToDateTime的调用,因为您已经比较了两个日期。您无需将日期值转换为日期。

答案 1 :(得分:1)

您的第一个Convert.ToDateTime()来电的参数是枚举;请确保使用.FirstOrDefault()Single()

对其进行后缀
objAdvList = objAdvList
            .Where(x => x.ToDate == Convert.ToDateTime(objAdvList
                .Where(y => y.FromDate == Convert.ToDateTime(objAdvList.Max(z => z.FromDate)))
                .Select(y => y.FromDate)
                .FirstOrDefault()
                ))
                .Select(x => x)
                .ToList();

答案 2 :(得分:0)

使用以下查询代替您的查询。

(objAdvList
            .Where(x => x.ToDate == Convert.ToDateTime(objAdvList
                .Where(y => y.FromDate == Convert.ToDateTime(objAdvList.Max(z => z.FromDate)))
                .Select(y => y.FromDate)
                ))
                .Select(x => x)).Single();

希望这有帮助。

答案 3 :(得分:0)

您的原始查询看起来有点奇怪,但这就是看起来像转换为LINQ over Entities,我认为:

var objAdvList=objAdvList
     .Where(a=>a.Filename.Contains("latest"))
     .Where(a=>a.ToDate==objAdvList
         .Where(a2=>EntityFunctions.TruncateTime(DateTime.Now)<=EntityFunctions.TruncateTime(a2.FromDate))
         .Where(a2=>EntityFunctions.TruncateTime(a2.FromDate)<=objAdvList
             .Where(a3=>EntityFunctions.TruncateTime(DateTime.Now)>=EntityFunctions.TruncateTime(a3.FromDate))
             .Max(a3=>a3.FromDate))
         .Min(a2=>a2.ToDate))

答案 4 :(得分:0)

感谢大家的快速回复。 通过使用你的帮助,我已经生成了我的Linq语句,它能够满足我的要求,取代下面给出的sql语句:

SELECT *
FROM   dbo.tbl_Advertisement
WHERE  ToDate = (SELECT min(ToDate)
                     FROM   dbo.tbl_Advertisement
                     WHERE  FromDate = (SELECT max(FromDate)
                                            FROM   dbo.tbl_Advertisement))

我的Linq声明如下:

objAdvList = objAdvList
            .Where(x => x.ToDate.Date == objAdvList
                .Where(y => y.FromDate.Date == objAdvList.Max(z => z.FromDate.Date))
                .Select(y => y.ToDate.Date).Min()
                )
                .Select(x => x)
                .ToList();

再次感谢, Mayank