从一个IEnumerable列表复制到另一个不起作用

时间:2019-04-26 09:39:25

标签: c#

我试图遍历一个可枚举列表,然后填充另一个空的可枚举列表。因此,基本上,第一个列表包含实体类的Enumerable列表,而我试图用此列表填充我的viewmodel。由于某种原因,尽管我可以看到包含值的实体列表,但我看不到将值分配给viewmodel列表。我在做什么错

var fundClasses = GetService<FUND_CLASS>().Where(x => x.LEGAL_FUND_CLASS.FUND_ID == id);

IEnumerable<AccountingFundClassesViewModel> accountingFundClassesViewModel = new List<AccountingFundClassesViewModel>();

foreach (var fc in fundClasses)
{
    foreach (var af in accountingFundClassesViewModel)
    {
        af.Id = fc.ID;
        af.Description = fc.DESCRIPTION;
        af.InvestedAmount = fc.INVESTED_AMMOUNT;
        af.InceptionDate = fc.INCEPTION_DATE;
        af.LegalFundClassId = fc.LEGAL_FUND_CLASS_ID;
        af.DataReference = fc.DATA_REFERENCE;
        af.CogencyClassId = fc.COGENCY_CLASS_ID;
        af.ClosureStatusId = fc.CLOSURE_STATUS_ID;
        af.IsSidePocket = fc.IS_SIDE_POCKET;
        af.IsThematic = fc.IS_THEMATIC;
        af.VehicleTypeId = fc.VEHICLE_TYPE_ID;
    }
}

var vm = new TermsCondtionsViewModel
{
    accountingFundClassesViewModel = accountingFundClassesViewModel
};

return vm;

视图模型

public class TermsCondtionsViewModel
{
    public IEnumerable<AccountingFundClassesViewModel> accountingFundClassesViewModel;

    public FundTermsViewModel fundTermsViewModel;

    public LegalFundClassesViewModel legalFundClassesViewModel;
}


public class AccountingFundClassesViewModel
{
    public int Id { get; set; }
    public string Description { get; set; }
    public decimal? InvestedAmount { get; set; }
    public DateTime? InceptionDate { get; set; }
    public int? LegalFundClassId { get; set; }
    public int? DataReference { get; set; }
    public int? CogencyClassId { get; set; }
    public int? ClosureStatusId { get; set; }
    public bool IsSidePocket { get; set; }
    public bool IsThematic { get; set; }
    public int? VehicleTypeId { get; set; }
}

1 个答案:

答案 0 :(得分:2)

您正在枚举accountingFundClassesViewModel,它是空的。 您想要的是迭代包含数据的数组,并创建一个要填充的新模型,然后将其添加到新列表中。

foreach (var fc in fundClasses)
{
    var af = new AccountingFundClassesViewModel
    {
        Id = fc.ID,
        Description = fc.DESCRIPTION,
        InvestedAmount = fc.INVESTED_AMMOUNT,
        InceptionDate = fc.INCEPTION_DATE,
        LegalFundClassId = fc.LEGAL_FUND_CLASS_ID,
        DataReference = fc.DATA_REFERENCE,
        CogencyClassId = fc.COGENCY_CLASS_ID,
        ClosureStatusId = fc.CLOSURE_STATUS_ID,
        IsSidePocket = fc.IS_SIDE_POCKET,
        IsThematic = fc.IS_THEMATIC,
        VehicleTypeId = fc.VEHICLE_TYPE_ID,
    };

    accountingFundClassesViewModel.Add(af);
}

或者,您可以使用LINQ,如@Jon在其评论中提到的那样。

var accountingFundClassesViewModel = fundClasses.Select(fc => new AccountingFundClassesViewModel{
        Id = fc.ID,
        Description = fc.DESCRIPTION,
        InvestedAmount = fc.INVESTED_AMMOUNT,
        InceptionDate = fc.INCEPTION_DATE,
        LegalFundClassId = fc.LEGAL_FUND_CLASS_ID,
        DataReference = fc.DATA_REFERENCE,
        CogencyClassId = fc.COGENCY_CLASS_ID,
        ClosureStatusId = fc.CLOSURE_STATUS_ID,
        IsSidePocket = fc.IS_SIDE_POCKET,
        IsThematic = fc.IS_THEMATIC,
        VehicleTypeId = fc.VEHICLE_TYPE_ID,
    }).ToList();