2015-10-15 197 views
0

我是MVC中的新成员。 Linq是在数据库中进行查询的系统。但我现在对Linq有困难。任何人都知道如何将我的SQL语句转换为Linq?如何将MVC sql查询转换为Linq查询?

我的实体,独立的上下文

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

此外,您还可以使用LINQ拉姆达表达式太:'localDB.summaries.Where(x => localDB.accounts.Any(y => y.studentID == x.studentID && y.username ==“username123”));' –

回答

1

在猜测不知道你的实体:

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

它是不同的上下文。 localDB for summary and accountDB for username 即时通讯有错误: System.NotSupportedException:指定的LINQ表达式包含对与不同上下文关联的查询的引用。 –

+0

你从来没有在你的问题中指定 – Ric

+0

我很抱歉那个先生。我现在编辑细节并添加它。 –

0

我假设表已经映射与学生的学生证我的意思是有一个外键关系的基础上,因此通过假设该账户:

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

我在我的问题中添加了一些信息。我必须上下文。 localDB用于摘要,accountDB用于帐户。 –