0

How can I write the below SQL query in linq?

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

What I have done is:

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))

However, it is throwing an error.

zuhair
  • 21
  • 5
  • Perhaps my [SQL to LINQ Recipe](https://stackoverflow.com/questions/49245160/sql-to-linq-with-multiple-join-count-and-left-join/49245786#49245786) might help you. – NetMage Aug 13 '20 at 21:55

1 Answers1

0

In method syntax:

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)

This returns an IEnumerable. If you are expecting a single result use Single instead of Where. You can also use First to take the first result from that set.

Alan Cheung
  • 244
  • 1
  • 7