Click here to Skip to main content
15,906,626 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
I am having trouble selecting total.
I am trying to find all orders where the order total is less than 500.00

Data Source:
public static class Customers
    {
        public static List<Customer> CustomerList { get; } =
            (from e in XDocument.Parse(InputValues.CustomersXml).Root?.Elements("customer")
                select new Customer
                {
                    CustomerId = (string) e.Element("id"),
                    CompanyName = (string) e.Element("name"),
                    Address = (string) e.Element("address"),
                    City = (string) e.Element("city"),
                    Region = (string) e.Element("region"),
                    PostalCode = (string) e.Element("postalcode"),
                    Country = (string) e.Element("country"),
                    Phone = (string) e.Element("phone"),
                    Orders = (
                        from o in e.Elements("orders").Elements("order")
                        select new Order
                        {
                            OrderId = (int) o.Element("id"),
                            OrderDate = (DateTime) o.Element("orderdate"),
                            Total = (decimal) o.Element("total")
                        }).ToArray()
                }).ToList();
    }


What I have tried:

List<Customer> customers = Customers.CustomerList;

            IEnumerable<(string, int, decimal)> res = from w in customers
                                                      where //I need to have here: Total(w.Total does not work) < 500.00M
                                                      select (w.Orders);

            return res;
Posted
Updated 28-Feb-22 23:35pm

1 solution

Try:
C#
IEnumerable<Order> smallOrders = customers
    .SelectMany(c => c.Orders)
    .Where(o => o.Total < 500);
Or:
C#
IEnumerable<Order> smallOrders = from c in customers
                                 from o in c.Orders
                                 where o.Total < 500
                                 select o;

Edit: For your altered return type:
C#
IEnumerable<(string customerId, int orderId, decimal total)> smallOrders 
    = customers
    .SelectMany(c => c.Orders.Select(o => (c.CustomerId, o.OrderId, o.Total)))
    .Where(o => o.Total < 500);
or:
C#
IEnumerable<(string customerId, int orderId, decimal total)> smallOrders 
    = from c in customers
      from o in c.Orders
      where o.Total < 500
      select (c.CustomerId, o.OrderId, o.Total);
 
Share this answer
 
v2
Comments
LuckyChloe 1-Mar-22 5:47am    
I am getting this error
Error CS0266 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<linq.datasources.order>' to 'System.Collections.Generic.IEnumerable<(string customerId, int orderId, decimal total)>'. An explicit conversion exists (are you missing a cast?)
Richard Deeming 1-Mar-22 6:02am    
IEnumerable<Order> is not the same thing as IEnumerable<(string, int, decimal)>. If you change the return type, then you need to change the select to match.
LuckyChloe 1-Mar-22 5:54am    
https://ibb.co/C2m6pqZ
like this
LuckyChloe 1-Mar-22 6:05am    
I fixed it myself. Thank you Mr. Richard without your help I would not be able to fix it
Maciej Los 1-Mar-22 12:03pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900