S
Siegfried Heintze
The following code compiles and runs but does not produce the correct
output. I get the same product names listed over and over again.
I want to create customer objects that have a list of orders that in turn,
have a list of order-details. I'm using the famous Northwind Sample Database
on SQL Express. I am indeed getting the desired data structures with the
wrong contents.
I did some google searching for examples but could not find an example of a
double join that exploits this cool feature of grouping.
I'm trying to extrapolate from
http://msdn.microsoft.com/en-us/library/bb397905(v=VS.100).aspx.
Thanks,
Siegfried
class LINQNorthwindDemoMain
{
static System.IO.TextWriter outp = System.Console.Out;
static System.IO.TextReader inp = System.Console.In;
static void Main(string[] args)
{
LINQNorthwindDemoDataContext dc = new LINQNorthwindDemoDataContext();
var customers = from c in dc.Customers
join order in dc.Orders on c.CustomerID equals order.CustomerID into
myOrders
from subOrder in myOrders
join detail in dc.Order_Details on subOrder.OrderID equals detail.OrderID
into myDetails
select new
{
CompanyName = c.CompanyName,
orders = from o in myOrders
select new { details = from d in myDetails
select new { name=d.Product.ProductName, q=d.Quantity } }
};
foreach (var c in customers)
{
outp.Write("co={0} ", c.CompanyName);
int ii = 0;
foreach (var order in c.orders)
{
outp.Write("{0}", (ii++ == 0 ? "[" : ", "));
int jj=0;
foreach(var detail in order.details){
outp.Write("{0}{1}/{2}",(jj++==0?"[":", "), detail.name, detail.q);
}
outp.Write("]");
}
outp.Write("]");
outp.WriteLine();
}
}
}
output. I get the same product names listed over and over again.
I want to create customer objects that have a list of orders that in turn,
have a list of order-details. I'm using the famous Northwind Sample Database
on SQL Express. I am indeed getting the desired data structures with the
wrong contents.
I did some google searching for examples but could not find an example of a
double join that exploits this cool feature of grouping.
I'm trying to extrapolate from
http://msdn.microsoft.com/en-us/library/bb397905(v=VS.100).aspx.
Thanks,
Siegfried
class LINQNorthwindDemoMain
{
static System.IO.TextWriter outp = System.Console.Out;
static System.IO.TextReader inp = System.Console.In;
static void Main(string[] args)
{
LINQNorthwindDemoDataContext dc = new LINQNorthwindDemoDataContext();
var customers = from c in dc.Customers
join order in dc.Orders on c.CustomerID equals order.CustomerID into
myOrders
from subOrder in myOrders
join detail in dc.Order_Details on subOrder.OrderID equals detail.OrderID
into myDetails
select new
{
CompanyName = c.CompanyName,
orders = from o in myOrders
select new { details = from d in myDetails
select new { name=d.Product.ProductName, q=d.Quantity } }
};
foreach (var c in customers)
{
outp.Write("co={0} ", c.CompanyName);
int ii = 0;
foreach (var order in c.orders)
{
outp.Write("{0}", (ii++ == 0 ? "[" : ", "));
int jj=0;
foreach(var detail in order.details){
outp.Write("{0}{1}/{2}",(jj++==0?"[":", "), detail.name, detail.q);
}
outp.Write("]");
}
outp.Write("]");
outp.WriteLine();
}
}
}