With SQL I would do the following…
SELECT G.* FROM GOAL G
INNER JOIN Plan P ON
P.planID = G.planID
INNER JOIN User U ON
U.userID = P.userID
WHERE U.userID = @userID
In my Controller (MVC4) "ActionResult Edit" Im doing the following, which is two queries, how could I do just one? The goal is to make sure the user is only able to do the “Edit” action on their own data vs. someone else’s goal.
var plan = db.Plan.Where(b => b.UserID == CurrentUserID).FirstOrDefault();
Goal goal = db.Goals
.Where(b => b.PlanID == plan.ID)
.FirstOrDefault();
return View(goal);
Model
public class Plan
{
public int ID { get; set; }
[Required]
public int UserID { get; set; }
}
[Table("PlanGoal")]
public class Goal
{
public int ID { get; set; }
[Required]
public int PlanID { get; set; }
}
Or you could join Goals and Plan together instead of adding a property to your
Goalclass: