Selecting in LINQ
Now, use object instead of integer or string.
public class Bird
{
public string Name { get; set; }
public string Color { get; set; }
public int Sightings { get; set; }
}
Initiate the list with birds.
List<Bird> birds = new List <Bird> {
new Bird { Name = "Beo", Color = "Red", Sightings = 3 },
new Bird { Name = "Merpati", Color = "White", Sightings = 2}
}
birds.Add( new Bird { name = "Kakaktua", Color = "Black", Sightings = 2};
var jalak = new { Name = "Jalak", Color = "Yellow", Sightings = 4 };
birds.Add(jalak);
// result: error
birds.Add ( new Bird { Name = jalak.Name, Color = jalak.Color, Sightings = Jalak.Sightings });
The way to select a certain bird.
from b in birds where b.Color == "Red" select b;
// result: { Name: "Beo", Color: "Red", Sightings = 3}
from b in birds where b.Name == "Kakaktua" select b.Color;
// result: Black
from b in birds where b.Color == "White" select new { b.Name, b.Color };
// result: { Name: Merpati, Color: White }