Run Code  | API  | Code Wall  | Misc  | Feedback  | Login  | Theme  | Privacy  | Patreon 

Fórum ➡ GroupJoin'ing Books and Orders ♦

//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

// aljodav
// http://rextester.com/users/376

// in reply to the MSDN Forum thread
// http://bit.ly/2ebf3Vg

namespace Rextester
{
    using T2=Tuple<int,string>;
    using T3=Tuple<int,int,string>;
    using BooksList=List<Tuple<int,string>>;
    using OrdersList=List<Tuple<int,int,string>>;
    class Program
    {
        static IEnumerable<T2> GetBooks(){ // Book ➡ BookId, Title
            yield return Tuple.Create(1,"DevCurry Tips");
            yield return Tuple.Create(2,".NET & COM");
            yield return Tuple.Create(3,"jQuery Recipes");
            yield return Tuple.Create(4,"Motivational Gurus");
            yield return Tuple.Create(5,"Spiritual Gurus");
            yield break;
        }
        static IEnumerable<T3> GetOrders(){ // Order ➡ OrderId, BookId, PaymentMode
            yield return Tuple.Create(1,1,"Cheque");
            yield return Tuple.Create(2,5,"Credit");
            yield return Tuple.Create(3,1,"Cash");
            yield return Tuple.Create(4,3,"Cheque");
            yield return Tuple.Create(5,3,"Cheque");
            yield return Tuple.Create(6,4,"Cash");
            yield break;
        }
        
        public static void Main(string[] args)
        {
            IEnumerable<T2> outerBooks=GetBooks();
            IEnumerable<T3> innerOrders=GetOrders();
            
            outerBooks
                .GroupJoin(innerOrders,
                    book=>book.Item1,
                    order=>order.Item2,
                    (book,orders)=>new{
                        Title=book.Item2,
                        OrderIds=orders.Select(x=>x.Item1),
                        PaymentMode=orders.Select(x=>x.Item3)
                    })
                .ToList()
                .ForEach(x=>{
                    Console.Write("\n\nBook Title: {0}\n\tOrders Id: ",x.Title);
                    x.OrderIds.ToList().ForEach(y=>Console.Write("{0} ",y));
                    Console.Write("\n\tPayment Mode: ");
                    x.PaymentMode.ToList().ForEach(y=>Console.Write("{0} ",y));
                });
            
            Console.WriteLine("\n\nHello, world!");
        }
    }
}
 run  | edit  | history  | help 0