LINQ Nested Query

4eigner

Member
Oct 30, 2012
117
0
76
My question is, what am I missing?

Order Class has a List<Product> OrderItems with ProdID, Name, Price properties.

I'm trying to find the items that OrderID 48 has ordered.

Code:
var ordVar2 = from o in orders
                           where o.OrderID == 48
                           select new
                          {
                              o.OrderID,
                              pId = o.OrderItems.Select(p => p.ProdID),
                              pName = o.OrderItems.Select(p => p.Name),
                              pPrice = o.OrderItems.Select(p => p.Price)
                          };

foreach (var oInfo1 in ordVar2)
            {
                Console.WriteLine("Order ID: " + oInfo1.OrderID + "\nProducts Ordered \nProduct ID: " + oInfo1.pId + "\nProduct Name: " + oInfo1.pName + "\nProduct Price: " + oInfo1.pPrice);
            }

My output:

Order ID: 48
Products Ordered
Product ID: System.Linq.Enumerable+WhereSelectListIterator`2[Lab5.Product,System.Int32]
Product Name: System.Linq.Enumerable+WhereSelectListIterator`2[Lab5.Product,System.String]
Product Price: System.Linq.Enumerable+WhereSelectListIterator`2[Lab5.Product,System.Double]
Press any key to continue.

Yes, it's homework, I would like to know where I'm misunderstanding the query.
 

Mr Evil

Senior member
Jul 24, 2015
464
187
116
mrevil.asvachin.com
When you do something like
Code:
"some string" + anObject
it will be compiled to
Code:
"some string" + anObject.ToString()
If anObject is an enumerable, it will end up as what you've got in your output. You might want to look at String.Join() to see how to turn all the items in an enumerable into a string.
 

sao123

Lifer
May 27, 2002
12,653
205
106
Code:
 select new
                          {
                              o.OrderID,
                              pId = o.OrderItems.Select(p => p.ProdID),
                              pName = o.OrderItems.Select(p => p.Name),
                              pPrice = o.OrderItems.Select(p => p.Price)
                          };


pId, pName, pPrice are also collections in this case, you must iterate over each of them...

Try something like:

Code:
var Stuff = from O in AllOrders where O.OrderID == 48
            from P in O.Items select new
            {
                O = O.OrderID,
                pID = P.ProdID,
                pName = P.Name,
                pPrice = P.Price,
                pQty = P.Quantity
            };


Then later you can:
foreach(var X in Stuff)
            {
                Console.WriteLine(X.O + " " + X.pID + " " + X.pName + " " + X.pPrice + " " + X.pQty);
            }
 

Merad

Platinum Member
May 31, 2010
2,586
19
81
It's also perfectly legal (and I would argue more logical for most people) to nest anonymous classes so that you end up with each order containing a collection of items. Also, when you know your result is a single object (e.g., lookup by id) you should use one of First/FirstOrDefault/Single/SingleOrDefault depending on the exact semantics of the query.

Code:
var orders = allOrders.Select(o => new 
  {
    o.OrderId,
    Items = o.OrderItems.Select(p => new
    {
      p.ProdId,
      p.Name,
      p.Price
    })
  })
  .FirstOrDefault(o => o.OrderId == 48);
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |