1

I want to generate a LINQ statement like this type of SQL statement

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

I have generated Linq statement as followes:

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

Where objAdvList is a List type collection of

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

But this code give me an error message as followes:

Unable to cast object of type 'WhereSelectListIterator`2[AdvertisementAccess,System.DateTime]' to type 'System.IConvertible'.

Is there anyone to solve my problem?

Regards, Mayank

4

5 回答 5

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,因为您已经在比较两个日期。您不需要将日期值转换为日期。

于 2013-07-08T12:40:18.530 回答
1

您第一次Convert.ToDateTime()调用的参数是一个枚举;确保使用.FirstOrDefault()or为它添加后缀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();
于 2013-07-08T12:40:05.307 回答
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();

希望这可以帮助。

于 2013-07-08T12:41:03.503 回答
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))
于 2013-07-08T18:27:38.233 回答
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();

再次感谢,马扬克

于 2013-07-09T05:08:16.643 回答