平面数据仅将最后一个对象保存在最后

时间:2020-10-14 19:03:20

标签: c# collections

我想将此数据放到视图模型中,但是我不确定该怎么做。下面的示例仅将最后一个listOfChild数据保存到视图模型中。我正在使用foreach,是否应该使用其他可以收集数据的东西?如果我在每个方法的第二个方法中添加add方法,则不会添加最后一条记录。

        List<Subscription> ListOfSubscriptions = new List<Subscription>();
        List<SubscriptionChild> SubscriptionChild = new List<SubscriptionChild>();
        SubscriptionChild.Add(new SubscriptionChild() { ChildProductId = 1, ChildProductName = "Child 1" });
        SubscriptionChild.Add(new SubscriptionChild() { ChildProductId = 2, ChildProductName = "Child 2" });
        ListOfSubscriptions.Add(new Subscription() { SubscriptionId = 1, ParentProductName = "Product 1",ListOfSubscriptionChild= SubscriptionChild });
        SubscriptionChild.Clear();
        ListOfSubscriptions.Add(new Subscription() { SubscriptionId = 2, ParentProductName = "Product 2"});
        SubscriptionChild.Clear();
        SubscriptionChild.Add(new SubscriptionChild() { ChildProductId = 3, ChildProductName = "Child 3" });
        SubscriptionChild.Add(new SubscriptionChild() { ChildProductId = 4, ChildProductName = "Child 4" });
        ListOfSubscriptions.Add(new Subscription() { SubscriptionId = 1, ParentProductName = "Product 3", ListOfSubscriptionChild = SubscriptionChild });


        List<SubscriptionViewModel> SubscriptionViewModel = new List<SubscriptionViewModel>();
        foreach (var Subscription in ListOfSubscriptions)
        {
            SubscriptionViewModel vm = new SubscriptionViewModel();
            vm.ParentProductId = Subscription.ParentProductId;
            vm.ParentProductName = Subscription.ParentProductName;
            var listOfChild = Subscription.ListOfSubscriptionChild.ToList();
            foreach (var item in listOfChild)
            {
                vm.ChildProductId = item.ChildProductId;
                vm.ChildProductName = item.ChildProductName;
            }
            SubscriptionViewModel.Add(vm);
        }


    class Subscription
    {
        public int SubscriptionId { get; set; }
        public int ParentProductId { get; set; }
        public string ParentProductName { get; set; }
        public List<SubscriptionChild> ListOfSubscriptionChild { get; set; }
    }
    class SubscriptionChild
    {
        public string ChildProductName { get; set; }
        public int ChildProductId { get; set; }
    }
    class SubscriptionViewModel
    {
        public int SubscriptionId { get; set; }
        public int ParentProductId { get; set; }
        public string ParentProductName { get; set; }
        public string ChildProductName { get; set; }
        public int ChildProductId { get; set; }
    }

1 个答案:

答案 0 :(得分:1)

您只应该在代码中添加一行:

foreach (var Subscription in ListOfSubscriptions)
    {
        SubscriptionViewModel vm = new SubscriptionViewModel();
        vm.ParentProductId = Subscription.ParentProductId;
        vm.ParentProductName = Subscription.ParentProductName;
        var listOfChild = Subscription.ListOfSubscriptionChild.ToList();
        foreach (var item in listOfChild)
        {
            vm.ChildProductId = item.ChildProductId;
            vm.ChildProductName = item.ChildProductName;
            SubscriptionViewModel.Add(vm);
        }
       
    }