Here is a simple example using LINQ projection. It takes a List<string> that include space-delimited strings, queries them and then project into an IEnumerable<Person>. I then loop through the output sequence and write the results to the console.
class Program
{
static void Main(string[] args)
{
List<string> people = new List<string>
{ "Joe Smith 20", "Jim Jackson 30", "Jason Suzuki 40" };
IEnumerable<Person> query =
from p in people
select new Person
{
FirstName = p.Split(' ')[0],
LastName = p.Split(' ')[1],
Age = Convert.ToInt32(p.Split(' ')[2])
};
foreach (Person p in query)
{
Console.WriteLine("First Name:\t{0}\r\nLastName:\t{1}\r\nAge:\t\t{2}",
p.FirstName, p.LastName, p.Age);
}
}
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
No comments:
Post a Comment