Ronika Jency
Write a program using LinQ with two lists - The first list contains Order Items while the second list contains Order Items count. The output is - order item with count
By Ronika Jency in LINQ on May 30 2019
  • Mahesh Alle
    Jul, 2019 23

    See the below code:-

    1. class Order
    2. {
    3. public int Id;
    4. public string Name;
    5. }
    6. class OrderItem
    7. {
    8. public int Id;
    9. public string Name;
    10. public int OrderId;
    11. }
    12. public class Program
    13. {
    14. static void Main(string[] args)
    15. {
    16. var orders = new List<Order> {
    17. new Order { Id = 1, Name = "One" } ,
    18. new Order { Id = 2, Name = "Two" }
    19. };
    20. List<OrderItem> orderItems = new List<OrderItem>
    21. {
    22. new OrderItem { Id = 1, Name = "Item One", OrderId = 1 },
    23. new OrderItem { Id = 2, Name = "Item Two", OrderId = 1 },
    24. new OrderItem { Id = 3, Name = "Item Three", OrderId = 1 },
    25. new OrderItem { Id = 4, Name = "Item Four", OrderId = 2 },
    26. new OrderItem { Id = 5, Name = "Item Five", OrderId = 2 }
    27. };
    28. var results = from order in orders
    29. join orderItem in orderItems on order.Id equals orderItem.OrderId into GroupOrderItems
    30. select new { order, GroupOrderItems };
    31. //Just to write the results to the console
    32. string columns = "{0,-20} {1, -20}";
    33. Console.WriteLine(string.Format(columns, "Order", "Item Count"));
    34. foreach (var result in results)
    35. {
    36. Console.WriteLine(columns, result.order.Name, result.GroupOrderItems.Count());
    37. }
    38. Console.ReadLine();
    39. }
    40. }

    • 1
  • Ronika Jency
    May, 2019 30

    class Order { public int Id; public string Name; public Order(int id, string name) { this.Id = id; this.Name = name; } } class OrderItem { public int Id; public string Name; public int OrderId; public OrderItem(int id, string name, int orderId) { this.Id = id; this.Name = name; this.OrderId = orderId; } } class Program { static void Main(string[] args) { List orders = new List() { new Order(1, "one"), new Order(2, "two") }; List orderItems = new List() { new OrderItem(1, "itemOne", 1), new OrderItem(2, "itemTwo", 1), new OrderItem(3, "itemThree", 1), new OrderItem(4, "itemFour", 2), new OrderItem(5, "itemFive", 2) }; var joined = from o in orders join oi in orderItems on o.Id equals oi.OrderId into gj // gj means group join and is a collection OrderItem select new { o, gj }; // this is just to write the results to the console string columns = "{0,-20} {1, -20}"; Console.WriteLine(string.Format(columns, "Order", "Item Count")); foreach (var j in joined) { Console.WriteLine(columns, j.o.Name, j.gj.Count()); } } }

    • 1
  • kanu mathur
    Jan, 2022 9

    public static void LinqJoin()
    {
    IList orderItems = new List() {
    new OrderItems(){orderId=1, orderName=”Soap”},
    new OrderItems(){orderId=2, orderName=”Shampoo”},
    new OrderItems(){orderId=3, orderName=”Moisturiser”}
    };
    IList orderCount = new List(){
    new OrderCount(){orderId=1, count=3},
    new OrderCount(){orderId=2, count=4},
    new OrderCount(){orderId=3, count=8}
    };
    var joinResult= from items in orderItems
    join oc in orderCount
    on items.orderId equals oc.orderId
    select new {
    orderItem=items.orderName,
    count= oc.count
    } ;
    foreach(var item in joinResult)
    {
    Console.WriteLine(item.orderItem + “ :”+ item.count);
    }
    }

    1. public class OrderCount
    2. {
    3. public int orderId { get; set; }
    4. public int count { get; set; }
    5. }
    6. public class OrderItems
    7. {
    8. public int orderId{ get; set; }
    9. public string orderName{ get; set; }
    10. }

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS