Icer Exiomo
Icer Exiomo

Reputation: 49

How can I convert a MVC sql query to a Linq query?

I am new in MVC. Linq is the system of making queries in database. but I am having difficulties right now with Linq. Anyone know how to convert my sql statement to Linq?

My entities, seperate context

localDB.summaries

accountDB.account


SELECT * FROM summary 
WHERE studentID = 
(SELECT studentID FROM accounts WHERE username = 'username123')

FROM user IN localDB.summaries
WHERE -------- please guide my linq--------
SELECT user

Upvotes: 0

Views: 147

Answers (2)

Varun Vasishtha
Varun Vasishtha

Reputation: 461

I am assuming that accounts table has been mapped with student on the basis of student id I mean there is a foreign key relationship so by assuming that:

var data = localDB.summaries.where(c=>c.Students.Account.FirstorDefault().username == "username123")

Upvotes: 0

Ric
Ric

Reputation: 13248

At a guess without knowing your entities:

var query = from user in localDB.summaries
join account in localDB.accounts on user.studentID equals account.studentID
where account.username == "username123"
select user;

Upvotes: 1

Related Questions