using System;
using System.Linq;
namespace pc.linq04
{
class Person
{
public string
FirstName { get; set; }
public string LastName
{ get; set; }
public Person(string
FirstName, string LastName)
{
this.FirstName = FirstName;
this.LastName = LastName;
}
}
class Program
{
static void Main(string[] args)
{
var ppl = new Person[] { new Person("Pawel", "Chooch"),
new Person("Magda", "Tyvoniuk"), new Person("Tomek","Chook"),
new Person("Marin","Smartzic"), new Person("Jake","Tyvoniuk")};
var result1 = from p in ppl
select new { FullName = $"{p.FirstName} {p.LastName}" };
var result2 = ppl.Select(p => new { FullName = $"{p.FirstName} {p.LastName}" });
Array.ForEach(result1.ToArray(), p => Console.Write(p.FullName+ ", "));
Console.WriteLine();
Array.ForEach(result2.ToArray(), p => Console.Write(p.FullName+ ", "));
Console.WriteLine();
}
}
}
Of course before we project our objects we can filter them
using System;
using System.Linq;
namespace pc.linq04
{
class Person
{
public string
FirstName { get; set; }
public string LastName
{ get; set; }
public Person(string
FirstName, string LastName)
{
this.FirstName = FirstName;
this.LastName = LastName;
}
}
class Program
{
static void Main(string[] args)
{
var ppl = new Person[] { new Person("Pawel", "Chooch"),
new Person("Magda", "Tyvoniuk"), new Person("Tomek","Chooch"),
new Person("Marin","Smartzic"), new Person("Jake","Tyvoniuk")};
var result1 = from p in ppl
where p.LastName == "Chooch"
select new { FullName = $"{p.FirstName} {p.LastName}" };
var result2 = ppl.Where(p=> p.LastName == "Tyvoniuk")
.Select(p => new { FullName = $"{p.FirstName} {p.LastName}" });
Array.ForEach(result1.ToArray(), p => Console.Write(p.FullName+ ", "));
Console.WriteLine();
Array.ForEach(result2.ToArray(), p => Console.Write(p.FullName+ ", "));
Console.WriteLine();
}
}
}