2013-10-19 118 views
0

如何對實體框架執行此sql查詢?實體框架上的SQL查詢

select 
id, 
column1, 
column2 = case when (select max(column2) from table1 b where b.id = a.id) = a.column2 
then 'Positive' else 'Negative' end 
from table1 a 
+0

你的意思怎麼寫到LINQ到實體還是要執行,雖然這的DbContext SQL命令? – jannagy02

+0

是的,如何將它寫入Linq To Entities – Mel

+0

對不起,但我只是閱讀了sql命令,並且我收到了一些通知。 (從table1 b中選擇max(column2),其中b.id = a.id)。由於在哪裏使用,它總是會有一條記錄。所以第2列總是'正面'。 (如果id真的是我的預期的主鍵) – jannagy02

回答

1
var query = from t in context.table1 
      let column2_temp = context.table1.Where(p=>p.id==t.id).Max(p=>p.column2) 
      let column2 = column2_temp == t.column2? "Positive" : "Negative" 
      select new {t.id, column2}