Fórum ➡ GroupJoin'ing Books and Orders ♦
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
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(){
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(){
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
|
|
|