1

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
FROM Projet INNER JOIN
 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

asked Jul 27, 2017 at 10:00
6
  • your code looks like you are using EntityFramework... is that your ORM? Commented Jul 27, 2017 at 10:03
  • 1
    See msdn : code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b Commented Jul 27, 2017 at 10:04
  • Possible duplicate of C# Joins/Where with Linq and Lambda Commented Jul 27, 2017 at 10:05
  • @DarkSquirrel42 yes i am using EntityFramework and my ORM too Commented Jul 27, 2017 at 10:06
  • please show your Entities Project and UtilisateurInProjet ... did you set up a navigation property for the relation you want to use? Commented Jul 27, 2017 at 10:08

3 Answers 3

0

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();
answered Jul 27, 2017 at 10:47
Sign up to request clarification or add additional context in comments.

2 Comments

I understand much better this syntax. Thank you for your help.
No problem Kamil
0

Is this you want,

public JsonResult GetProjsName(int id)
{
 db.Configuration.ProxyCreationEnabled = false;
 List<Projet> liprojs = db.Projets.Join(db.UtilisateurInProjet,projet=> projet.ProjetId,utilisateurInProjet => utilisateurInProjet.ProjetId,(projet,utilisateurInProjet) => new {projet.ProjetId, projet.Libelle, utilisateurInProjet.UtilisateurId} ).Where(utilisateurInProjet.UtilisateurId==id).ToList();
 return Json(liprojs, JsonRequestBehavior.AllowGet);
}
answered Jul 27, 2017 at 10:17

Comments

0

since your navigation properties remain unknown this is more or less a wild guess...

public JsonResult SomeMethod(int id)
 {
 db.Configuration.ProxyCreationEnabled = false;
 return Json(db.UtilisateurInProjet.Where(x=>x.id==id).SelectMany(x=>x.Projects.Select(p=>new { ProjetId=p.ProjetId, Libelle=p.Libelle, UtilisateurInProjet=x.UtilisateurId})).ToList(), JsonRequestBehavior.AllowGet);
 }
answered Jul 27, 2017 at 10:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.