Click here to Skip to main content
15,886,795 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I solved the problem by using the combination of both comprehension syntax and lambda syntax. I was wondering how can I write the following code completely in lambda syntax format (without using from etc.) since I feel like it is not a good practice to use both at the same time - please correct me if I am wrong.

Thank You

What I have tried:

public static IEnumerable<(string customerId, int orderId, DateTime orderDate)> Res()
        {
            List<Customer> customers = Customers.CustomerList;

            string region = "WA";

            IEnumerable<(string, int, DateTime)> res = (from c in customers
                                                       from o in c.Orders
                                                       where c.Region == region
                                                       select (c.CustomerId, o.OrderId, o.OrderDate)).Take(3);

            return res;


        }
Posted
Updated 2-Mar-22 2:41am
v2

1 solution

By using a very minor modification of the answer I posted yesterday:
C#
IEnumerable<(string, int, DateTime)> res = customers
    .Where(c => c.Region == region)
    .SelectMany(c => c.Orders.Select(o => (c.CustomerId, o.OrderId, o.OrderDate)))
    .Take(3);
 
Share this answer
 
Comments
LuckyChloe 2-Mar-22 10:11am    
This is very useful. Thank you very much❤️
Maciej Los 2-Mar-22 10:59am    
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