0

I have an SQL-Statement that I have to transfer to Entity Framework:

select * from logbuch where institutionid = @id OR userid IN (SELECT id from benutzerkonto where institution_id = @id)

I have as EntityFramework:

context.logbuch.Where(x => x.institutionid == int.Parse(benutzerkontoID) || x.userid ???INSIDE SUBSELECT???).ToList();

I am very new in EF & LINQ and I don't know how it is possiple to integrate the subselect-condition right side of OR.

Can someone help me realising this in a EntityFramework-Query?

Thanks for helping!

comidos
  • 123
  • 1
  • 12
  • 1
    `|| context,benutzerkonto.Where(b => b.institution_id == int.Parse(benutzerkontoID).Select(b => b.id).Contains(x.userid) ` – sTrenat Jul 10 '20 at 11:22
  • Try following : context.logbuch.Select(x => x.institutionid == int.Parse(benutzerkontoID) || context.benutzerkonto.Where(y => y.institution_id == x.institutionid).FirstOrDefault()).ToList() – jdweng Jul 10 '20 at 11:27
  • @sTrenat: Thank you, after repairing one missing bracket, its working! – comidos Jul 10 '20 at 11:39
  • @comidos, FYI: the question is not on EF, rather it is on LINQ/Lambda expressions. – Srinika Pinnaduwage Jul 10 '20 at 11:49
  • @jdweng: I tried this too, but didn't get it running! Operator isn't accepted for this comparison (bool <=> object) – comidos Jul 10 '20 at 11:49
  • @SrinikaPinnaduwage: Sorry, like i said, i am very new in this part! – comidos Jul 10 '20 at 11:51
  • context.logbuch.Select(x => (x.institutionid == int.Parse(benutzerkontoID))? x : context.benutzerkonto.Where(y => y.institution_id == x.institutionid)).ToList() – jdweng Jul 10 '20 at 12:28
  • 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. Basically define sub-select as separate query variable and use `Contains`. – NetMage Jul 10 '20 at 19:26

0 Answers0