我目前在运行时出现以下错误,无法编译,并且不确定是什么问题
我得到的错误是在GetAllocationsViewModel方法的这一行上
var allocationsGrouped = Mapper.Map<ILookup<string,FIRMWIDE_MANAGER_ALLOCATION>, ILookup<string,FirmWideAllocationsViewModel>>(GetAllocationsGrouped(EntityType.Firm, firm.ID, d));
InnerException {“无法转换类型为'System.Collections.Generic.List1 [BSG.Manager.WebUI.ViewModels.Allocations.FirmWideAllocationsViewModel]'的对象”以键入'System.Linq.IGrouping2 [BSG.Manager.WebUI.ViewModels .Allocations.FirmWideAllocationsViewModel]'。“}
System.Exception {System.InvalidCastException}
不确定为什么我返回ILookup时无法显示类型为'System.Collections.Generic.List的对象。在编译时似乎没有问题。与AutoMapper有关吗
代码
private AllocationsViewModel GetAllocationsViewModel(int id, DateTime date)
{
var ms = GetStrategy(id);
DateTime d = new DateTime(date.Year, date.Month, 1).AddMonths(1).AddDays(-1);
if (ms.FIRM_ID != null)
{
var firm = GetService<FIRM>().Get(ms.FIRM_ID.Value);
var currentEntity = new EntityAllocationsViewModel(new EntityViewModel { EntityId = firm.ID, EntityName = firm.NAME, EntityType = EntityType.Firm });
//var allocationsGrouped = Mapper.Map<List<FIRMWIDE_MANAGER_ALLOCATION>, List<FirmWideAllocationsViewModel>>(GetAllocationsGrouped(EntityType.Firm, firm.ID, d).ToList());
var allocationsGrouped = Mapper.Map<ILookup<string,FIRMWIDE_MANAGER_ALLOCATION>, ILookup<string,FirmWideAllocationsViewModel>>(GetAllocationsGrouped(EntityType.Firm, firm.ID, d));
var vm = new AllocationsViewModel
{
CurrentEntity = currentEntity,
//ManagerAllocations = allocationsGrouped,
// MissingProducts = missingProducts
};
return vm;
}
return null;
}
protected ILookup<string, FIRMWIDE_MANAGER_ALLOCATION> GetAllocationsGrouped(EntityType entity, int id, DateTime date)
{
_logger.Info($"GetAllocationsGrouped entity:{entity} id:{id} date:{date}");
var allocations = IoC.Resolve<IPackageRepo>().GetManagerAllocationsGrouped(entity, id, date);
return allocations;
}
ILookup<string, FIRMWIDE_MANAGER_ALLOCATION> IPackageRepo.GetManagerAllocationsGrouped(EntityType entity, int id, DateTime date, int? count)
{
var allocations = GetManagerAllocations(entity, id, date);
var allocationsLookup = allocations.ToLookup(a => a.MANAGER_STRATEGY_NAME, a => a);
// add grouping by entity
var allocationsGrouped = GroupManagerAllocationsByEntity(entity, allocations);
var strategyTotals = allocationsGrouped.GroupBy(mgrStrat => new { mgrStrat.MANAGER_STRATEGY_ID }).Select(s => new { MANAGER_STRATEGY_ID = s.Key.MANAGER_STRATEGY_ID, StrategyTotal = s.Sum(x => x.UsdEmv) }).ToDictionary(k => k.MANAGER_STRATEGY_ID, v => v.StrategyTotal);
var sum = allocationsGrouped.Sum(a => a.EMV);
foreach (IGrouping<string, FIRMWIDE_MANAGER_ALLOCATION> item in allocationsLookup)
{
var allocationsGroup = item.Select(a => a);
foreach (var alloc in allocationsGroup)
{
if (sum > 0 && strategyTotals[alloc.MANAGER_STRATEGY_ID] != 0)
{
alloc.Percent = alloc.EMV / sum;
alloc.GroupPercent = alloc.EMV / strategyTotals[alloc.MANAGER_STRATEGY_ID];
}
}
}
return allocationsLookup;
}
自动映射器代码
public static void Initialize()
{
Mapper.Initialize((config) =>
{
config.ReplaceMemberName("FIRM_ID", "FirmID");
config.ReplaceMemberName("FIRM_NAME", "FirmName");
config.ReplaceMemberName("MANAGER_STRATEGY_ID", "ManagerStrategyID");
config.ReplaceMemberName("MANAGER_STRATEGY_NAME", "ManagerStrategyName");
config.ReplaceMemberName("MANAGER_ACCOUNTING_CLASS_ID", "ManagerAccountClassID");
config.ReplaceMemberName("MANAGER_ACCOUNTING_CLASS_NAME", "ManagerAccountingClassName");
config.ReplaceMemberName("MANAGER_FUND_ID", "ManagerFundID");
config.ReplaceMemberName("MANAGER_FUND_NAME", "ManagerFundName");
config.ReplaceMemberName("MANAGER_FUND_OR_CLASS_ID", "ManagerFundOrClassID");
config.ReplaceMemberName("NAV", "Nav");
config.ReplaceMemberName("PRODUCT_ID", "ProductID");
config.ReplaceMemberName("PRODUCT_NAME", "ProductName");
config.ReplaceMemberName("QUANTITY", "Quantity");
config.ReplaceMemberName("EVAL_DATE", "EvalDate");
config.CreateMap<FIRMWIDE_MANAGER_ALLOCATION, FirmWideAllocationsViewModel>().ReverseMap();
});
}
模型
public class FIRMWIDE_MANAGER_ALLOCATION
{
private decimal _groupPercent;
public int FIRM_ID { get; set; }
public string FIRM_NAME { get; set; }
public int? MANAGER_STRATEGY_ID { get; set; }
public int? MANAGER_FUND_ID { get; set; }
public int MANAGER_ACCOUNTING_CLASS_ID { get; set; }
public int? MANAGER_FUND_OR_CLASS_ID { get; set; }
public string MANAGER_FUND_NAME { get; set; }
public string MANAGER_ACCOUNTING_CLASS_NAME { get; set; }
public string MANAGER_STRATEGY_NAME { get; set; }
public int? PRODUCT_ID { get; set; }
public string PRODUCT_NAME { get; set; }
public int? QUANTITY { get; set; }
public decimal? NAV { get; set; }
public DateTime EVAL_DATE { get; set; }
public int? DEFAULT_STRATEGY_ID { get; set; }
public string DEFAULT_STRATEGY_NAME { get; set; }
public decimal USD_EMV { get; set; }
//needed for kendo aggregates
public decimal UsdEmv => USD_EMV;
public decimal EMV { get; set; }
public decimal WEIGHT { get; set; }
[NotMapped]
public decimal Percent { get; set; } // manual calc
[NotMapped]
public decimal Sort
{
get
{
return PRODUCT_NAME == "Other"
? decimal.MaxValue
: USD_EMV;
}
}
[NotMapped]
public decimal GroupPercent
{
get { return _groupPercent; }
set { _groupPercent = value; }
}
}
public class FirmWideAllocationsViewModel
{
public List<string> Hierarchy
{ get; set; }
private decimal _groupPercent;
public int FirmID { get; set; }
public string FirmName { get; set; }
public int? ManagerStrategyID { get; set; }
public int? ManagerFundID { get; set; }
public int ManagerAccountClassID{ get; set; }
public int? ManagerFundOrClassID { get; set; }
public string ManagerFundName { get; set; }
public string ManagerAccountingClassName { get; set; }
public string ManagerStrategyName { get; set; }
public int? ProductID { get; set; }
public string ProductName { get; set; }
public int? Quantity { get; set; }
public decimal? Nav { get; set; }
public DateTime EvalDate { get; set; }
public int? DefaultStrategyID { get; set; }
public string DEFAULT_STRATEGY_NAME { get; set; }
public decimal Usd_Emv { get; set; }
//needed for kendo aggregates
public decimal UsdEmv => Usd_Emv;
public decimal Emv { get; set; }
public decimal Weight { get; set; }
[NotMapped]
public decimal Percent { get; set; } // manual calc
[NotMapped]
public decimal Sort
{
get
{
return ProductName == "Other"
? decimal.MaxValue
: Usd_Emv;
}
}
[NotMapped]
public decimal GroupPercent
{
get { return _groupPercent; }
set { _groupPercent = value; }
}