I have a document in RavenDB that contains a child collection. The child collection contains the following base type.
public class Section
{
public string BackgroundColor { get; set; }
public string Description { get; set; }
public string DesktopBackgroundImageUrl { get; set; }
public DateTime? EndDate { get; set; }
public string MobileBackgroundImageUrl { get; set; }
public int SortOrder { get; set; }
public DateTime? StartDate { get; set; }
public string TextColor { get; set; }
public string Title { get; set; }
public SectionType Type { get; set; }
}
There are a few derived classes from this type, one of which being this.
public class OfferSection : Section
{
public IEnumerable<Merchant> Merchants { get; set; }
}
The problem I'm having is that I need to query this child collection and get the documents that contain the derived type and then query it's values.
This is where I've got to so far, however because it's using the base type, the Merchants property doesn't exist
public class Hubs_ByMerchantId : AbstractIndexCreationTask<Hub>
{
public Hubs_ByMerchantId()
{
Map = hubs => from hub in hubs
select new
{
Sections_Merchants_Id = hub.Sections.Where(x => x.Type == SectionType.Offer).SelectMany(x => x.Merchants.Select(y => y.Id))
};
}
}
Can anyone point me in the right direction?
Thanks
You can use .OfType<Type> to get what ypou want:
hub.Sections.OfType<OfferSection>().SelectMany(x => x.Merchants.Select(y => y.Id));
After a bit of messing about the way I got this to work was by casting it inside the select. This worked because we already have a type enum saved against the section so it's easy enough to do the cast.
public class Hubs_ByMerchantId : AbstractIndexCreationTask<Hub>
{
public Hubs_ByMerchantId()
{
Map = hubs => hubs.SelectMany(x => (IEnumerable<OfferSection>)x.Sections).Where(x => x.Type == SectionType.Offer).Select(x => new
{
Sections_Merchants_Id = x.Merchants.Select(y => y.Id)
});
}
}
This probably isn't the best solution but it was the only thing that worked
Related
I am trying to map fields of an object with child collection of source using Automapper through ProjectTo IQueryable as following:
var map2 = cfg.CreateMap<SourceModel, DestinationModel>();
map2.ForMember(fieldName, opt => opt.MapFrom(source => source.CustomFieldValues.FirstOrDefault(f => f.Name == fieldName).Value));
Models are as following:
public class SourceModel
{
public IEnumerable<CustomFieldValue> CustomFieldValues { get; set; }
}
public class CustomFieldValue
{
public string Name { get; set; }
public string Value { get; set; }
}
public class DestinationModel
{
public string _CUSTOM_Test { get; set; }
public int _CUSTOM_Mynumber { get; set; }
public DateTime _CUSTOM_mydate { get; set; }
public bool _CUSTOM_mybool { get; set; }
public decimal _CUSTOM_numberdec { get; set; }
public int _CUSTOM_numint { get; set; }
public int _CUSTOM_numper { get; set; }
public DateTime _CUSTOM_mydate2 { get; set; }
public DateTime _CUSTOM_mydate3 { get; set; }
public DateTime _CUSTOM_mydate4 { get; set; }
public int _CUSTOM_mynum2 { get; set; }
}
Expected Result:
As the collection contains only string values but I need to map with different datatypes based on field name.
Actual Result:
But when I try to apply conversion then queryable throws exception because sql query doesn't support this conversion.
You can use AutoMapper's Custom Type Converters.
AutoMapper does not know about any mapping from string to int for example so to create maps for these types, we must supply a custom type converter. This can be achieved by using ConvertUsing() method.
Mapper.Initialize(configuration =>
{
configuration.CreateMap<string, int>().ConvertUsing(s => Convert.ToInt32(s));
configuration.CreateMap<string, DateTime>().ConvertUsing(s => new DateTimeTypeConverter().Convert(s));
configuration.CreateMap<string, bool>().ConvertUsing(s => Convert.ToBoolean(s));
configuration.CreateMap<string, decimal>().ConvertUsing(s => Convert.ToDecimal(s));
configuration.CreateMap<SourceModel, DestinationModel>()
.ForMember("_CUSTOM_Mynumber", opt => opt.MapFrom(src => src.CustomFieldValues.FirstOrDefault(x => x.Name == "_CUSTOM_Mynumber").Value));
});
The example above shows how we can convert int, bool, and decimal. For DateTime, we'll use ITypeConverter.
public interface ITypeConverter<in TSource, TDestination>
{
TDestination Convert(TSource source);
}
Then define a custom convertion:
public class DateTimeTypeConverter : ITypeConverter<string, DateTime>
{
public DateTime Convert(source)
{
return Convert.ToDateTime(source);
}
}
This is my class which holds database data:
public partial class PermissionGroup
{
public int Id { get; set; }
public string Name { get; set; }
// other database properties
public virtual ICollection<GroupActionPermission> GroupActionPermissions { get; set; }
}
And that's my dto's:
public class PermissionGroupDTO
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<GroupActionPermissionDTO> ActionPermissions { get; set; }
}
public class GroupActionPermissionDTO
{
public int Id { get; set; }
public int GroupId { get; set; }
public int PermissionActionId { get; set; }
public PermissionGroupDTO Group { get; set; }
}
Now, I am making mapping:
public IEnumerable<PermissionGroupDTO> GetGroups()
{
return OnConnect<IEnumerable<PermissionGroupDTO>>(db =>
{
return db.PermissionGroups
.Include(i => i.GroupActionPermissions)
.ProjectTo<PermissionGroupDTO>()
.ToList();
});
}
And I am getting collection of PermissionGroupDTO which should contains collection of GroupActionPermissionDTO, but that collection stays null. Is there something wrong with my code? I am afraid that automapper can map collections from foreign keys.
Also, thats my automapper initializer:
Mapper.Initialize(cfg =>
{
cfg.CreateMap<PermissionGroup, PermissionGroupDTO>();
cfg.CreateMap<GroupActionPermission, GroupActionPermissionDTO>();
});
I believe the reason is desribed here http://docs.automapper.org/en/stable/Queryable-Extensions.html
Note that for this feature to work, all type conversions must be explicitly handled in your Mapping.
So that means you should manually configure the mapping:
Mapper.Initialize(cfg =>
{
cfg.CreateMap<PermissionGroup, PermissionGroupDTO>()
.ForMember(dto => dto.ActionPermissions , conf => conf.MapFrom(ol => ol.GroupActionPermissions )));;
cfg.CreateMap<GroupActionPermission, GroupActionPermissionDTO>();
});
BTW, note that fields are named differently: GroupActionPermissions vs. ActionPermissions. This is also the reason why automapper doesn't map it automatically and then you should use the manual configuration I wrote.
I want to map source class to derived (from abstract) destination classes depend on value of some property.
I have the following source classes:
public partial class ApplicationDriver
{
public virtual ICollection<ApplicationDriverEquipment> Equipments { get; set; }
}
public partial class ApplicationDriverEquipment
{
public int Id { get; set; }
[StringLength(256)]
public string Make { get; set; }
[StringLength(256)]
public string Model { get; set; }
[StringLength(256)]
public string Year { get; set; }
[StringLength(256)]
public string VINNumber { get; set; }
[StringLength(256)]
public string PlateNumber { get; set; }
[StringLength(256)]
public string CurrentMileage { get; set; }
[StringLength(256)]
public string Length { get; set; }
public string Type { get; set; }
public int DriverId { get; set; }
public virtual ApplicationDriver Driver { get; set; }
}
I want to map to the following classes, depend on Type parameter:
public class ApplicationDriverDomain
{
public List<ApplicationDriverEquipmentAbstractDomain> Equipments { get; set; }
}
public abstract class ApplicationDriverEquipmentAbstractDomain
{
public int Id { get; set; }
public string Make { get; set; }
public string Model { get; set; }
public string Year { get; set; }
public string PlateNumber { get; set; }
public string CurrentMileage { get; set; }
public string Type { get; protected set; }
}
public class ApplicationDriverEquipmentTractorDomain : ApplicationDriverEquipmentAbstractDomain
{
public ApplicationDriverEquipmentTractorDomain()
{
Type = ApplicationDriverEquipmentTypeStaticStringsDomain.Tractor;
}
public string VINNumber { get; set; }
}
public class ApplicationDriverEquipmentTrailerDomain : ApplicationDriverEquipmentAbstractDomain
{
public ApplicationDriverEquipmentTrailerDomain()
{
Type = ApplicationDriverEquipmentTypeStaticStringsDomain.Trailer;
}
public string Length { get; set; }
}
public class ApplicationDriverEquipmentStraightTruckDomain : ApplicationDriverEquipmentAbstractDomain
{
public ApplicationDriverEquipmentStraightTruckDomain()
{
Type = ApplicationDriverEquipmentTypeStaticStringsDomain.StraightTruck;
}
public string VINNumber { get; set; }
public string Length { get; set; }
}
public class ApplicationDriverEquipmentCargoVanDomain : ApplicationDriverEquipmentAbstractDomain
{
public ApplicationDriverEquipmentCargoVanDomain()
{
Type = ApplicationDriverEquipmentTypeStaticStringsDomain.CargoVan;
}
public string VINNumber { get; set; }
public string Length { get; set; }
}
I try to do it:
ApplicationDriverEquipmentAbstractDomain GetEquipment(Infrastructure.Asset.ApplicationDriverEquipment infrastructure)
{
ApplicationDriverEquipmentAbstractDomain result = null;
var config = new MapperConfiguration(cfg => cfg.AddProfile<AutoMapperApplicationModel>());
var mapper = config.CreateMapper();
switch (infrastructure.Type)
{
case ApplicationDriverEquipmentTypeStaticStringsDomain.Tractor:
result = mapper.Map<ApplicationDriverEquipmentTractorDomain>(infrastructure);
break;
case ApplicationDriverEquipmentTypeStaticStringsDomain.Trailer:
result = mapper.Map<ApplicationDriverEquipmentTrailerDomain>(infrastructure);
break;
case ApplicationDriverEquipmentTypeStaticStringsDomain.StraightTruck:
result = mapper.Map<ApplicationDriverEquipmentStraightTruckDomain>(infrastructure);
break;
case ApplicationDriverEquipmentTypeStaticStringsDomain.CargoVan:
result = mapper.Map<ApplicationDriverEquipmentCargoVanDomain>(infrastructure);
break;
}
return result;
}
CreateMap<Infrastructure.Asset.ApplicationDriverEquipment, ApplicationDriverEquipmentTractorDomain>();
CreateMap<Infrastructure.Asset.ApplicationDriverEquipment, ApplicationDriverEquipmentTrailerDomain>();
CreateMap<Infrastructure.Asset.ApplicationDriverEquipment, ApplicationDriverEquipmentStraightTruckDomain>();
CreateMap<Infrastructure.Asset.ApplicationDriverEquipment, ApplicationDriverEquipmentCargoVanDomain>();
CreateMap<Infrastructure.Asset.ApplicationDriverEquipment, ApplicationDriverEquipmentAbstractDomain>()
.Include<Infrastructure.Asset.ApplicationDriverEquipment, ApplicationDriverEquipmentTractorDomain>()
.Include<Infrastructure.Asset.ApplicationDriverEquipment, ApplicationDriverEquipmentTrailerDomain>()
.Include<Infrastructure.Asset.ApplicationDriverEquipment, ApplicationDriverEquipmentStraightTruckDomain>()
.Include<Infrastructure.Asset.ApplicationDriverEquipment, ApplicationDriverEquipmentCargoVanDomain>()
.ForMember(dest => dest.Type, opt => opt.ResolveUsing(GetEquipment))
;
CreateMap<Infrastructure.Asset.ApplicationDriver, ApplicationDriverDomain>()
.ForMember(dest => dest.Equipments, opt => opt.MapFrom(src => src.Equipments));
but I got an error:
"Error mapping types.\r\n\r\nMapping types:\r\nApplicationDriver ->
ApplicationDriverDomain\r\nInfrastructure.Asset.ApplicationDriver ->
Domain.POCO.Application.ApplicationDriverDomain\r\n\r\nType Map
configuration:\r\nApplicationDriver ->
ApplicationDriverDomain\r\nInfrastructure.Asset.ApplicationDriver ->
Domain.POCO.Application.ApplicationDriverDomain\r\n\r\nProperty:\r\nEquipments"
Updated:
So I believe I understand what you are trying to do, and apologies I may have slightly led you down the incorrect route. You flow is basically to distinguish what infrastructure type the source object is and then create that type of object. Also you need to understand the two different Mapper set up ways.
In the first part of your code you are trying to set it up with an instance of the Mapper but then using my Static style of using the Mapper.Map I would recommend always using the static style so that you have the ability to do some more dynamic ways of pulling mapping profiles in.
Mapper.Initialize(cfg => cfg.AddProfile<AutomapperRules>());
var domain = Mapper.Map<Domain.ApplicationDriverEquipmentTractorDomain>(inf);
Next you only need to reference that a mapping type from the underlying source to the domain types in your profile i.e.
CreateMap<ApplicationDriverEquipmentInfrastructure, ApplicationDriverEquipmentTractorDomain>();
CreateMap<ApplicationDriverEquipmentInfrastructure, ApplicationDriverEquipmentTrailerDomain>();
CreateMap<ApplicationDriverEquipmentInfrastructure, ApplicationDriverEquipmentStraightTruckDomain>();
CreateMap<ApplicationDriverEquipmentInfrastructure, ApplicationDriverEquipmentCargoVanDomain>();
Then what you need to do is to call your GetEquipment method from the mapping that describes the ApplicationDriver i.e.
CreateMap<ApplicationDriver, ApplicationDriverDomain>()
.ForMember(dest => dest.Equipments, opt => opt.ResolveUsing(x => x.Equipments.Select(GetEquipment)));
private ApplicationDriverEquipmentAbstractDomain GetEquipment(ApplicationDriverEquipmentInfrastructure infrastructure)
{
switch (infrastructure.Type)
{
case "Tractor":
return Mapper.Map<ApplicationDriverEquipmentTractorDomain>(infrastructure);
case "Trailer":
return Mapper.Map<ApplicationDriverEquipmentTrailerDomain>(infrastructure);
case "StraightTruck":
return Mapper.Map<ApplicationDriverEquipmentStraightTruckDomain>(infrastructure);
case "CargoVan":
return Mapper.Map<ApplicationDriverEquipmentCargoVanDomain>(infrastructure);
}
return null;
}
Example Usage:
Mapper.Initialize(cfg => cfg.AddProfile<AutomapperRules>());
var inf = new ApplicationDriverEquipmentInfrastructure()
{
CurrentMileage = "mil",
Length = "123",
Make = "ccc",
Model = "15",
Type = "Tractor",
VINNumber = "vin"
};
var driver = new ApplicationDriver()
{
Equipments = new List<ApplicationDriverEquipmentInfrastructure>() {inf}
};
var domain = Mapper.Map<ApplicationDriverDomain>(driver);
Inheritance in AM works by checking the type of the source, not by using a discriminator. That's what you were supposed to understand from the docs. One way to solve your problem is to pass an existing destination to Map. Created by smth like the GetEquipment method you have there. ApplyBaseMapping is a hack, you use Include/IncludeBase to reuse configuration. Unfortunately you've also hit a bug already fixed in the MyGet build, so the real error was kind of hidden from you. The only way to debug this in your version is by checking the execution plan.
I'm struggling using AutoMapper to map these objects.
Here are my DTO:
public class ContainerDTO
{
public List<MoneyAccountDTO> MoneyAccounts { get; set; }
public List<CardDTO> Cards { get; set; }
}
public class MoneyAccountDTO
{
public string Iban { get; set; }
}
public class CardDTO
{
public string MoneyAccountIban { get; set; }
}
Here are the BusinessObject:
public class Container
{
public List<MoneyAccount> MoneyAccounts { get; set; }
}
public class MoneyAccount
{
public string Iban { get; set; }
public List<Card> Cards { get; set; }
}
public class Card
{
public string MoneyAccountIban { get; set; }
}
What i try to achieve here, is to find all CardDTO in the ContainerDTO that have the same Iban than a MoneyAccount and Create a list of these cards in the MoneyAccount itself.
The probleme here is that i receive the CardsDTO object at the same level as the MoneyAccountDTO it related to.
I started with something like that:
cfg.CreateMap<ContainerDTO, Container>()
.ForMember(dest => dest.MoneyAccounts, opt => opt.MapFrom(src => src.Cards.
Where(c => c.MoneyAccountIban == XXX)));
But i'm unable to replace those XXX by the Money account the mapper is enumerating. It think i'm not in the right direction.
I'm unable to find a good solution for this mapping.
Any Help will be appreciated.
The solution for this problem is to use a foreach with a where clause.
cfg.CreateMap<ContainerDTO, Container>().AfterMap((src, dest) => dest.MoneyAccounts
.ForEach(a => a.Cards = src.Cards
.Where(c => c.MoneyAccountIban == a.Iban)
.Select(d => Mapper.Map<CardDTO, ICard>(d)).ToList()));
It works like a charm !
I have two classes:
public class ClassA
{
public ClassA()
{
LanguageInfo = new List<ClassALanguage>();
}
public long ID { get; set; }
public string Value { get; set; }
public List<ClassALanguage> LanguageInfo { get; set; }
}
public class ClassALanguage
{
public long ID { get; set; }
public string Name { get; set; }
public string Language { get; set; }
}
And I have a destination class:
public class WSClassA
{
public long ID { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
So I thought I could configure the mapping like this:
Mapper.CreateMap<ClassA, WSClassA>()
.ForMember(
ws => ws.Name,
opt => opt.MapFrom(clsA => clsA.LanguageInfo.Find(lg => lg.Language == languageSelected).Name));
The problem is after the first time the mapping is executed, it is not able to change. Even if the CreateMap() is executed with another value for languageSelected, the binding works as the first time.
Is there any solution to accomplish that?
In your case you need some context when you perform your mapping - the selected language.
Instead of using MapFrom use the ResolveUsing method with a custom IValueResolver.
See - https://automapper.codeplex.com/wikipage?title=Custom%20Value%20Resolvers
When you make your .Map call use the overload which allows you to modify the IMappingOperationOptions. Set the language you want to compare against in the interfaces Items collection.
Finally in your custom IValueResolver's Resolve method you can access those items via ResolutionResult.Context.Options.Items property.