I can’t express what I want to do. Please help. Considering my code below:
void Main()
{
List<Person> person = new List<Person>
{
new Person { Name = "Maria Anders", Age = 21 },
new Person { Name = "Ana Trujillo", Age = 55 },
new Person { Name = "Thomas Hardy", Age = 40 },
new Person { Name = "Laurence Lebihan", Age = 18 },
new Person { Name = "Victoria Ashworth", Age = 16 },
new Person { Name = "Ann Devon", Age = 12 }
};
person.Select(x => new { x.Name, x.Age }).Dump();
}
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
I want to print the Name | Age | Status of a person.
Status is a derived column. Where it should have a value of either “Adult” if the person’s age is >= 18, otherwise “Under age”.
First you need to add a property named
Statusto classPerson.then you can loop into the list of person to do your action:
The reason of adding
Statusproperty and overridingToStringmethod is to put the logic in one place. Otherwise, suppose some day, you need to change “Under age” to “Nonage”, you won’t need to change the strings everywhere but only one place in yourPersonclass.