Search code examples
c#windows-phone-8datacontext

Using OnNavigatedTo for two table - Windows Phone 8


Evetything is fine with this code:

string selectedRowOrderId = "";
   if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedRowOrderId))
            {
                int selectedID = int.Parse(selectedRowOrderId);
                var query = from TblOrders order in orderDB.Orders
                            where order.OrderId == selectedID
                            select order;

                DataContext = new ObservableCollection<TblOrders>(query)[0];
            }

I am able to display order details on the page. But I would like to display Order Products (from TblProductsToOrder) on the same page as a list under the order details. I mean, DataContext has to have one more query from TblProductsToOrder like this:

DataContext = new ObservableCollection<TblProductsToOrder>(query2)[0];

Could you help me how to have these two in the DataContext an the same time?


Solution

  • If you cannot change the model to include the reference between the Tables, you have another solution to your problem:

    public class OrderDetailsAndProducts
    {
        public TblOrders Order { get; set; }
        public TblProductsToOrder OrderProduct { get; set; }
    }
    

    in your assignment to DataContext you should be doing like this

    OrderDetailsAndProducts orderDetails=
          new OrderDetailsAndProducts
          {
              Order = query.FirstOrDefault(),
              OrderProduct = query2.FirstOrDefault()
          };
    DataContext = orderDetails;
    

    From what i understand about your database schema you should have more Products in the Order if it is so the class to hold both should be more like this:

    public class OrderDetailsAndProducts
    {
        public Order Order { get; set; }
        public List<TblProductsToOrder> OrderProducts { get; set; }
    }
    

    in your assignment to DataContext you should be doing like this

    OrderDetailsAndProducts orderDetails=
          new OrderDetailsAndProducts
          {
              TblOrders = query.FirstOrDefault(),
              OrderProducts = query2.ToList()
          };
    DataContext = orderDetails;