description属性を取得するenum拡張メソッドを次に示します。ここで照会可能な投影中にAutoMapperを使用してintをenum記述にマップする方法はありますか?
public static string GetDescription(this Enum enumeration)
{
if (enumeration == null)
throw new ArgumentNullException();
var value = enumeration.ToString();
var type = enumeration.GetType();
var descriptionAttribute =
(DescriptionAttribute[]) type.GetField(value).GetCustomAttributes(typeof (DescriptionAttribute), false);
return descriptionAttribute.Length > 0 ? descriptionAttribute[0].Description : value;
}
は、ソースオブジェクトである:ここで
public class Account {
public int AccountId {get;set;}
public int AccountStatusId {get;set;}
}
は列挙型である:ここでは
public enum AccountStatus {
[Description("N/A")]
None,
[Description("OPEN")]
Open,
[Description("CLOSED")]
Closed,
[Description("BAD CREDIT")
Problem
}
は、宛先オブジェクトである:ここで
public class GetAccountResponse {
public int AccountId {get;set;}
public string Status {get;set;}
}
はに私の試みです地図(最新の非静的なオートアプリを使用) rバージョン)。これはEFの照会可能なプロジェクション中のものであることを覚ここで
_config = new MapperConfiguration(cfg => cfg.CreateMap<Account, GetAccountsResponse>()
.ForMember(dest => dest.Status,
opts => opts.MapFrom(src => ((AccountStatus) src.AccountStatusId).GetDescription())));
は、クエリがIQueryable<Account>
で投影である:
query.ProjectToList<GetAccountResponse>(_config);
これは私が得る例外です:
は、Expression