C#
Creating a new console application C# and write:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Person
{
public string Name { get; set; }
public string Code { get; set; }
}
class Book
{
public string Title { get; set; }
public int Price { get; set; }
public string PersonCode { get; set; }
}
class Program
{
static void Main(string[] args)
{
// array Person
Person[] persons = new Person[]
{
new Person(){ Name = "Evgen", Code = "EVG" },
new Person(){ Name = "Valera", Code = "VAL" },
new Person(){ Name = "Sergey", Code = "SER" },
};
// fill
Book[] books = new Book[]
{
new Book(){ Title = "Traveling in India", Price = 23 , PersonCode = "EVG"},
new Book(){ Title = "Smart Home and Devices", Price = 23 , PersonCode = "VAL"},
new Book(){ Title = "Wizard of the Mediterranean", Price = 10 , PersonCode = "EVG"},
new Book(){ Title = "Study C#", Price = 23 , PersonCode = "SER"},
new Book(){ Title = "Study Andriod", Price = 15 , PersonCode = "VAL"},
};
// GroupJoin adds the desired collection of books to each item persons
var result = persons.GroupJoin(books, // all books
item1 => item1.Code, // by this field we link class Person
item2 => item2.PersonCode, // by this field we link class Book
(person, relatedbooks) => // delegate with parameters (Person person, Book[] relatedbooks)
// in the delegate we return a new type
// class
// {
// string resultPersonName;
// Book[] resultBooks;
// }
new
{
resultPersonName = person.Name,
resultBooks = relatedbooks,
});
// let's see what happened
foreach (var newPerson in result)
{
// show on screen resultPersonName
Console.WriteLine("{0}", newPerson.resultPersonName);
// show on screen each book
foreach (Book book in newPerson.resultBooks)
{
Console.WriteLine(" {0}", book.Title);
}
// new line
Console.WriteLine("");
}
}
}
}