using System;
using System.Linq;
namespace pc.linq08
{
class Person
{
public string
FirstName { get; set; }
public string LastName
{ get; set; }
public int
DepartmentId { get; set; }
public Person(string
FirstName, string LastName, int DepartmentId)
{
this.FirstName = FirstName;
this.LastName = LastName;
this.DepartmentId = DepartmentId;
}
}
class Department
{
public int Id { get; set; }
public string Name { get; set; }
public Department(int Id, string Name)
{
this.Id = Id;
this.Name = Name;
}
}
class Program
{
static void Main(string[] args)
{
var ppl = new Person[] { new Person("Pawel", "Chooch",1),
new Person("Magda", "Tyvoniuk",2), new Person("Tomek","Chooch",3),
new Person("Marin","Smartzic",2), new Person("Jake","Tyvoniuk",1)};
var depts = new Department[] { new Department(1, "IT"),
new Department(2,"Finance"), new Department(3,"Operatios")};
var result1 = from p in ppl
from d in depts
where p.DepartmentId == d.Id
select new {
FullName = $"{p.FirstName} {p.LastName}",
Department =
d.Name,
Display = new Func<string>(
() => $"{p.FirstName} {p.LastName}: {d.Name}")
};
Array.ForEach(result1.ToArray(), pd => Console.WriteLine(pd.Display()));
var result2 = ppl.SelectMany(p =>
depts.Where(d => d.Id ==
p.DepartmentId)
.Select(d => new { FullName= $"{p.FirstName} {p.LastName}",
Department = d.Name,
Display=
new Func<string>(
()=> $"{p.FirstName} {p.LastName}: {d.Name}")}));
Array.ForEach(result2.ToArray(), pd => Console.WriteLine(pd.Display()));
}
}
}