2013-10-19 120 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}