如何编写嵌套的 linq 查询

How can I write the nested linq query

如何在 linq 中编写以下 SQL 查询?

select * from Employee where Email = (select User_Email from tbl_Login where User_Email='abc@demo.com' and User_Password = 'demo123')

我所做的是:

from tblemp in ctx.Employees where tblemp.Email = (from tblLogin in ctx.tbl_Login where (tblLogin.User_Email == login.User_Email && tblLogin.User_Password == login.User_Password))

但是,它抛出错误。

在方法语法中:

var employee = ctx.Employee
  .Where(e => e.Email == ctx.tbl_Login
     .Single(l => l.User_Email = "abc@demo.com" and l.User_Password = "demo123")
     .User_Email)

这个returns一个IEnumerable。如果您期望单个结果,请使用 Single 而不是 Where。您还可以使用 First 从该集合中获取第一个结果。