0

How do I do this sql query to entity framework?

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
4
  • Do you mean how to write it into Linq To Entities or do you want to execute this sql command though DbContext? Commented Oct 19, 2013 at 18:36
  • yes, how to write it on into Linq To Entities Commented Oct 20, 2013 at 6:19
  • Sorry, but I just read the sql command and I have some notification. (select max(column2) from table1 b where b.id = a.id). Beacuse of the where caluse it will always have one record in result. So the column2 always 'Positive'. (if id is really primary key as I expected) Commented Oct 20, 2013 at 8:04
  • your right, sorry, id is not the primary key, Commented Oct 20, 2013 at 14:26

1 Answer 1

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}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.