Queryable Extensions

Test Classes

public class Address
{
    public int Id { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
}

public class AddressDTO
{
    public int Id { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
}

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal? Credit { get; set; }
    public Address Address { get; set; }
    public Address HomeAddress { get; set; }
    public Address[] Addresses { get; set; }
    public ICollection<Address> WorkAddresses { get; set; }
 }

 public class CustomerDTO
 {
    public int Id { get; set; }
    public string Name { get; set; }
    public AddressDTO HomeAddress { get; set; }
    public IEnumerable<AddressDTO> WorkAddresses { get; set; }
    public string AddressCity { get; set; }
 }


Test Data

public List<Customer> GetCustomers()
{
    Customer customer = new Customer()
    {
        Id = 1,
        Name = "Timucin Kivanc",
        Credit = 234.7m,
        Address = new Address() { 
            City = "Istanbul", 
            Country = "Turkey", 
            Id = 1, 
            Street = "Istiklal cad." 
        },
        HomeAddress = new Address() { 
            City = "Istanbul", 
            Country = "Turkey", 
            Id = 2, 
            Street = "Istiklal cad." 
        },
        WorkAddresses = new List<Address>() { 
            new Address() { 
                City = "Istanbul", 
                Country = "Turkey", 
                Id = 5, 
                Street = "Istiklal cad." 
            },
            new Address() { 
                City = "Izmir", 
                Country = "Turkey", 
                Id = 6, 
                Street = "Konak" 
            }
        },
        Addresses = new List<Address>() { 
            new Address() { 
                City = "Istanbul", 
                Country = "Turkey", 
                Id = 3, 
                Street = "Istiklal cad." 
            },
            new Address() { 
                City = "Izmir", 
                Country = "Turkey", 
                Id = 4, 
                Street = "Konak" 
            }
        }.ToArray()
    };

    var customerList = new List<Customer>() { customer };

    return customerList;
}


Map

var customers = GetCustomers();

//Create a select expression
var customerDto = customers.AsQueryable().Project().To<CustomerDTO>().FirstOrDefault();

// It is useful for Entity Framework 
// var customers = context.Customers.Where(c => c.Name != null).Project().To<CustomerDTO>().ToList();

/*
// Handwriting Expression
var customerDto = customers.AsQueryable().Select(c => new CustomerDTO {
   Id = c.Id,
   Name = c.Name,
   AddressCity = c.Addresses.City
   HomeAddress = new AddressDTO {
       Id = c.HomeAddress.Id
       City = c.HomeAddress.City,
       Country = c.HomeAddress.Country
   },
   WorkAddresses = p.WorkAddresses.Select(adr => new AddressDTO {
       Id = adr.Id
       City = adr.City,
       Country = adr.Country
   })
}).FirstOrDefault();
*/

//result : 
// customerDto.Id == 1
// customerDto.Name == "Timucin Kivanc"
// customerDto.AddressCity == "Istanbul"
// customerDto.WorkAddresses.Count() == 2
...
//All properties mapped

Last edited Mar 28, 2014 at 11:31 AM by timucinkivanc, version 4