ListEx<T> ListExAutoMap<TResult,TSource>(ListEx<TSource> source)

The sample displays how a person list can be converted to a contact list. You can also add filters in your query
as shown in the .Where(Predicate<T> predicate) below.

ListEx<Person> personList = GenerateItems();
//map the person list to a contact list
ListEx<ContactPerson> people = new ListExAutoMap<ContactPerson, Person>(personList)
.Map(p => new ContactPerson()
{
    Age = p.Age,
    FirstName = p.FirstName,
    IdNumber = p.IdNumber,
    Surname = p.Surname,
    Email = string.Empty
})
.ToListEx();

Console.WriteLine(String.Format("collection returned {0} results", people.Count.ToString()));

//returns an enumerator that is be passed as argument to construct the new list
people = new ListEx<ContactPerson>
(
    new ListExAutoMap<ContactPerson, Person>(personList)
        .Map(p => new ContactPerson()
        {
            Age = p.Age,
            FirstName = p.FirstName,
            IdNumber = p.IdNumber,
            Surname = p.Surname,
            Email = "z.evans@caprisoft.co.za"
        })
        .ToListEx()
        .WhereEnumerator(contactPerson => contactPerson.Surname == "Evans")
);          

//or simple just let the AutoMap convert for you
people = new ListExAutoMap<ContactPerson, Person>(personList)
.Map(p => new ContactPerson()
{
    Age = p.Age,
    FirstName = p.FirstName,
    IdNumber = p.IdNumber,
    Surname = p.Surname,
    Email = "z.evans@caprisoft.co.za"
})
.ToListEx()
.Where(contactPerson => contactPerson.FirstName == "Zack");

Last edited Dec 14, 2010 at 2:29 PM by Caprisoft, version 2

Comments

No comments yet.