Kamil Kamil - 1 year ago 99
SQL Question

Convert SQL resquest to C# lambda Expression

I am trying to convert a sql request to a lambda expression but I only know how to do it with a where statement. This is my request :

SELECT Projet.ProjetId, Projet.Libelle, UtilisateurInProjet.UtilisateurId
UtilisateurInProjet ON Projet.ProjetId = UtilisateurInProjet.ProjetId
WHERE (UtilisateurInProjet.UtilisateurId = @UtilisateurId)

and @UtilisateurId would be the selected value from the DropDownList in the view.

In my controller, I have this code :

public JsonResult GetProjsName(int id)
db.Configuration.ProxyCreationEnabled = false;
List<Projet> liprojs = db.Projets.Where(x => x.ProjetId == id).ToList();
return Json(liprojs, JsonRequestBehavior.AllowGet);


and "id" is the selected value from the DropDownList in the view.
Thank you

Answer Source

Give this a try. I find it easier to use Linq query syntax for joins, rather than Linq extensions.

var liprojs = (from p in db.Projets
              join uip in db.UtilisateurInProjet on p.ProjetID equals uip.ProjetID
              where uip.UtilisateurId == utilisateurId 
              select new {p.ProjetId, p.Libelle, uip.UtilisateurId}).ToList();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download