1

I have database relationships as follows:

enter image description here

In C# I'm creating new order when someone adds a product to the cart. My problem is that I need data of products that are being added to the cart inside of gridview, like category name, length of the product etc etc. Orders class contains Icollection of OrderDetails, and I'm trying to retrieve data from those order details in which i store product id. So the questions here is, is there any way i do multiple joins here so that I may retrieve all the data i need from table(class in C#) products,product categories and categories when user adds the product to the cart so that he may see what he put in the cart.

I've tried to access other tables via:

Orders.OrderDetails.ElementAT(0).Product.ProductCategories.ElementAt(0)...

But this doesn't leads me anywhere... Does anyone understands what I'm trying to achieve here?

If anyone can help me out with this or point me into the right direction what should I do??

dbc
  • 104,963
  • 20
  • 228
  • 340
perkes456
  • 1,163
  • 4
  • 25
  • 49
  • Something like this? http://stackoverflow.com/questions/530925/linq-using-inner-join-group-and-sum – dbc Apr 17 '15 at 20:42
  • I'm using Order.OrderDetails as data source, I don't think I'll be able to place the result of the query into that ICollection?? – perkes456 Apr 17 '15 at 20:45
  • Since you're asking a c# question, maybe we would be able to help you more if you showed us a skeleton of your classes. Are they just POCOs, Entity Framework objects, or ?? – dbc Apr 17 '15 at 20:49
  • They are Entity framework objects... I'm using ADO.NET framework for ORM (for mapping my database...) – perkes456 Apr 17 '15 at 21:05

1 Answers1

1

Typically it's wise to create a class to represent the data you're hoping to show, and then use LINQ to create a projection of your data that matches that structure, something like this:

from o in context.Orders
from p in o.Products
select new CartGridItem
{
    Product = p,
    CategoryNames = p.Categories.Select(c => c.Name),
}
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315