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
Kamil
3161 gold badge3 silver badges14 bronze badges
-
your code looks like you are using EntityFramework... is that your ORM?DarkSquirrel42– DarkSquirrel422017年07月27日 10:03:59 +00:00Commented Jul 27, 2017 at 10:03
-
1See msdn : code.msdn.microsoft.com/101-LINQ-Samples-3fb9811bjdweng– jdweng2017年07月27日 10:04:14 +00:00Commented Jul 27, 2017 at 10:04
-
Possible duplicate of C# Joins/Where with Linq and LambdaSerg– Serg2017年07月27日 10:05:31 +00:00Commented Jul 27, 2017 at 10:05
-
@DarkSquirrel42 yes i am using EntityFramework and my ORM tooKamil– Kamil2017年07月27日 10:06:18 +00:00Commented 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?DarkSquirrel42– DarkSquirrel422017年07月27日 10:08:13 +00:00Commented Jul 27, 2017 at 10:08
3 Answers 3
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
Richard Boyce
4235 silver badges12 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Kamil
I understand much better this syntax. Thank you for your help.
Richard Boyce
No problem Kamil
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);
}
Comments
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
DarkSquirrel42
10.3k3 gold badges22 silver badges31 bronze badges
Comments
default