Skip to content

Expression Mappings

Dima Bezzubenkov edited this page Sep 4, 2021 · 9 revisions

There are 4 available types of mappings: Expression mapping, Parameterized Mapping, Query Mapping and Parameterized Query Mapping.

Expression Mapping is the simplest mapping from these 4. But in 90% of all cases, that's all that you need. In some rare cases you may need Parameterized, Query and Parameterized Query mappings.

Expression mapping is a simple expression and describes how one model can be mapped to another:

mappings.Add<Address, AddressModel>(x => new AddressModel
{
    Id = x.Id,
    BuildingNumber = x.BuildingNumber,
    City = x.City,
    State = x.State,
    Country = (Countries) x.Country,
    Street = x.Street,
    ZipCode = x.ZipCode
});

mappings.Add<Appartment, AppartmentShortModel>(x => new AppartmentShortModel
{
    Id = x.Id,
    Floor = x.Floor,
    IsLodge = x.IsLodge,
    Number = x.Number,
    Size = x.Size.ToString()
});

You can apply mapping to IQueryable by using IQueryable extension method AsQuery:

IQueryable<Appartments> query = ...;
var list = _qmService.AsQuery<Appartment, AppartmentShortModel>(query).ToList();

To see more advanced examples please check Advanced cases