I'm trying to get up to speed with MVC, linq, razor, etc. Some things are working and others I just hit dead ends that really discourage me. This is the first one I haven't been able to work out. I've researched every lead I can before asking this question here and it's my first, so please go easy. I'm sure the answer is right in front of my face, but I just can't see it. I could type 14 pages of things I've tried, but I'm trying to keep this concise and to the point. I've completely mangled my code, so I'm pulling much of this from memory and therefore, it's not so much about punctuation errors. I apologize the error codes are not exact as I'm pulling those from memory too.
I have this left join linq query in my ActionResult:
public ActionResult Index()
{
var orders = from o in db.Orders
join u in db.Users on o.UserId equals u.Id into ou
where o.UserId == uId
from o in ou.DefaultIfEmpty()
select o;
Return View(orders.ToList());
}
In the Index.cshtml I call the results by doing:
@foreach (var item in Model.Orders) {
@item.User.Name //(joined Users table)
@item.OrderNumber //(Orders table)
So far, so good. However, I want to display more than just this list on the Index page. So, near as I can tell, I need a ViewModel to display several other pieces of data (OrdersTotal,etc.) on the same page. However, when I stick my linq query above in my models in a:
public List<Order> GetOrders()
{
var orders = from o in db.Orders
join u in db.Users on o.UserId equals u.Id into ou
where o.UserId == uId
from o in ou.DefaultIfEmpty()
select o;
return orders.ToList();
}
It doesn't recognize the joined Users table, meaning it doesn't know item.User.Name in the cshtml. I then start a pandora's box of experiments that I can not make work, mainly in the select statement like:
select New //this complains about anonymous type
or
select New Order(o.OrderNumber,u.Id) //I can't remember the error here, but red squiggly lines
or
select New Order { OrderNumber = o.OrderNumber, UserName = u.Name} //VS complains about not referencing the Users table, it wants to put a UserName field or property stub in my model, which if I do that, then it errors in the cshtml again as I'm guessing because I don't have a UserName in my Orders table?
A couple times, I got the above queries to work, but then it throws an exception when trying to access the Users table again in the cshtml. My controller for the viewmodel looks like this:
public ActionResult Index()
{
var ordersummary = new OrdersSummary();
var viewModel = new OrderSummary
{
Orders = ordersummary.GetOrders(),
OrdersTotal = ordersummary.GetOrdersTotal(),
};
return View(viewModel);
}
My viewModel model looks like this:
public class OrderSummary
{
public List<Order> Orders { get; set; }
public decimal OrdersTotal { get; set; }
public virtual User Users { get; set; }
{
My Order model basically iterates through the different columns in the db with the same public virtual User Users like above which works fine with the list is pulled from the ActionResult.
public class Order
{
public int Id { get; internal set; }
public int UserId { get; set; }
public int OrderNumber { get; set; }
...
...
public virtual User Users { get; set; }
}
I can't help but think I'm doing something fundamentally wrong in my model or controller rather than in the syntax of the linq statement. Any help is much appreciated.
UPDATE: I've gone through a few of the tutorials now and I'm basically trying to mimic this page here (down where it starts at ViewModels, although my OrderSummary Model stems from the ShoppingCart GetCartItems,etc. above):
http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-8
However, the above tutorial requires no sql join. Imagine a ticket system where you have tickets (orders) and techs (users) and you're trying to assign tickets to techs (one tech per ticket, but some tickets may not have techs assigned to them yet aka null) and what I'm trying to create is the overview portion where you're looking at all the tickets and the techs assigned to those tickets in a table and then further statistics like Total # of Tickets or maybe eventually TechA has x # of Tickets. So, I need all the info from the orders database and simply the name from the users database and then be able to introduce multiple pieces of info on the Index page.
I notice some of the tutorials use an extra table to join the Ids. So, in my case an extra table that simply has OrderId and UserId. Is that maybe a better way to do the join ("join" for lack of the proper terminology)?