2012-08-09 57 views
-4

所需SQL相當於:SQL的LINQ語法

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) 

我已經試過這一點,但它給空,而應返回一行。

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

[什麼都有喲你嘗試過嗎?](http://WhatHaveYouTried.com) – 2012-08-09 15:00:05

+1

雖然你只有兩個問題有不被接受的答案,但由此產生的百分比在這裏反映不佳;考慮接受其他問題中的至少一個答案。 – KeithS 2012-08-09 15:06:50

+0

嘿謝謝keith S讓我意識到這一點和Juergen是的,我已經嘗試並更新了我的答案。在此回覆的幫助下,我們在這裏提供幫助! – user1511069 2012-08-10 10:51:55

回答

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; 
+0

中查看更新,並請在問題中查看更新 – user1511069 2012-08-10 09:00:02

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; 
+0

請在問題 – user1511069 2012-08-10 08:59:21

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