-4

所需的 SQL 等效项:

select * from polls where (id=(select max(id) from polls where publish_at=(select max(publish_at) from polls where publish_at<='2012-08-10 00:00:00')) and status=1 )

我已经尝试过了,但它给出了 Null 而它应该返回一行。

var qry = db.Polls.Where(p => p.id == db.Polls.Where(x => x.publish_at == db.Polls.Max(y => y.publish_at) && x.publish_at <= System.DateTime.Today).Max(x => x.id) && p.status.Equals(PollStatus.Active)).FirstOrDefault();
4

3 回答 3

1

一种方法是这样的:

db.polls.Where(p => p.id == polls.Where(x => x.publish_at == polls.Max(y => y.publish_at)).Max(x => x.id));

像这样的另一种方式:

from p in db.polls
where p.id == (from x in db.polls
               where x.id == (from y in db.polls
                              where y.publish_at == db.polls.Max(y => y.publish_at)
                              select y.id).Max())
               select x.id).Max())
select p;
于 2012-08-09T15:12:47.043 回答
0
var query = from p in context.Polls
            where p.id == (from p2 in context.Polls
                           where p2.publish_at == context.Polls.Max(x => x.publish_at)
                           select p2).Max(y => y.id)
            select p; 
于 2012-08-09T15:10:19.837 回答
0

这很好用:

var qry = db.Polls.Where(p => p.id == db.Polls.Where(x => x.publish_at == db.Polls.Where(y => y.publish_at<=System.DateTime.Today).Max(y=>y.publish_at)).Max(x => x.id) && p.status.Equals(PollStatus.Active)).FirstOrDefault(); 
于 2012-08-10T10:49:59.173 回答