Convert.ToDateTime in Linq

本文关键字:Linq in ToDateTime Convert | 更新日期: 2023-09-27 18:21:24

我想生成一个类似于这种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是的列表类型集合

class AdvertisementAccess
{
    public DateTime FromDate { get; set; }
    public DateTime ToDate { get; set; }
    public string FileName { get; set; }
    public string Path { get; set; }
}

但这段代码给了我一条错误消息,如下所示:

无法强制转换类型为的对象'WhereSelectListIterator `2[AvertisementAccess,System.DateTime]'到类型"System.IC可转换".

有人能解决我的问题吗?

谨致问候,Mayank

Convert.ToDateTime in Linq

您的内部查询结果是一组结果。但是,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的调用,因为您已经在比较两个日期了。您不需要将日期值转换为日期。

第一个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();

使用下面的查询而不是您的查询。

(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();

希望这能有所帮助。

您的原始查询看起来有点奇怪,但我认为这就是转换为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))

感谢大家快速而有用的回复。通过使用您的帮助,我已经生成了我的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