使用linq或Plain C#填充给定的对象结构

时间:2019-06-05 18:45:34

标签: c# linq

我正在尝试填充将包含LegalFundClassCommercialViewModelLegalFundClassSideLetterViewModel对象的AllTerms对象。

LEGAL_FUND_CLASS 表基本上具有父记录和子记录,并与 LegalParentClassId 列相关。一位父母有一个孩子。因此,我需要遍历 legalfundClasses 对象并填充 IEnumerable<LegalFundClassWrapper> AllTerms。 LegalFundClassDetailsViewModel表示 LEGAL_FUND_CLASS 表中的记录。因此, legalfundClasses 变量包含此 legal_fund_class 表中的记录。有几条记录。有些记录不会有子记录。我需要以将父记录添加到 LegalFundClassCommercialViewModel 并将其子记录添加到
的方式进行填充 LegalFundClassSideLetterViewModel 。包装器将包含父记录和子记录的集合,其中某些子记录将不存在,因此 LegalFundClassSideLetterViewModel 属性将为null。

有人可以给我一个想法吗?

C#

 public class LegalFundClassViewModel
    {
        public IEnumerable<LegalFundClassWrapper> AllTerms;


        public class LegalFundClassWrapper
        {
            public LegalFundClassDetailsViewModel LegalFundClassCommercialViewModel { get; set; }
            public LegalFundClassDetailsViewModel LegalFundClassSideLetterViewModel { get; set; }
        }

    }

    public class LegalFundClassDetailsViewModel
    {

        public string Description { get; set; }
        public string AuditSummary { get; set; }
        public string FeesReviewSummary { get; set; }
        public string TermsReviewSummary { get; set; }
        public int Id { get; set; }
        public int FundId { get; set; }
        public int FundClassType { get; set; }
        public int? CurrencyId { get; set; }
        public string PrimaryCurrencyName { get; set; }
        public string OtherCurrencyName { get; set; }
        public int? ManagerStrategyId { get; set; }
        public string ManagerStrategyName { get; set; }
        public int? SubVotingId { get; set; }
        public string SubVotingName { get; set; }
        public int? SubHotIssueId { get; set; }
        public string SubHotIssueName { get; set; }
        public int? RedsFrqncyId { get; set; }
        public string RedsFrqncyName { get; set; }
        public int? RedsNoticeDays { get; set; }
        public int? NoticeTypeOfDaysId { get; set; }
        public string NoticeTypeOfDaysName { get; set; }
        public int? LegalParentClassId { get; set; }
   }

 var managerStrategyFundIds = GetService<MANAGERSTRATEGY>().WhereWithIncludes<MANAGERSTRATEGY>(x => x.ID == managerStratedyId, x => x.FUNDs).SelectMany(x => x.FUNDs).Select(x => x.ID).ToList();  

var legalfundClasses = GetService<LEGAL_FUND_CLASS>().Where(x => managerStrategyFundIds.Contains(x.FUND_ID));

我正在尝试创建一个包含所有记录的列表。如何遍历并填充AllTerms

  var allFunds = legalfundClasses.Select(fc => new LegalFundClassDetailsViewModel
        {
            Description = fc.DESCRIPTION,
            Id = fc.ID,
            FundId = fc.FUND_ID,
            FundClassType = fc.CLASS_TYPE,
            AuditSummary = getAuditSummary(managerStratedyId, fc.ID),
            FeesReviewSummary = getFeesReviewSummary(fc),
            TermsReviewSummary = getTermsReviewSummary(fc),
            CurrencyId = fc.CURRENCY_ID,
        });



public class LegalFundClassViewModel
    {
        public IEnumerable<LegalFundClassWrapper> AllTerms;


        public class LegalFundClassWrapper
        {
            public LegalFundClassDetailsViewModel 
         LegalFundClassCommercialViewModel { get; set; }
            public LegalFundClassDetailsViewModel 
          LegalFundClassSideLetterViewModel { get; set; }
        }

如下图所示,有两条记录。在legal_parent_class id字段中具有值的记录是该记录之上的记录的子记录。如果您注意到顶部记录的ID与底部记录的legal_parent_class_id相匹配。

识别孩子并将记录相应存储在相应属性中的最佳方法是什么

enter image description here

1 个答案:

答案 0 :(得分:0)

您需要left outer join。就您而言

var allFunds = new[]
{
    new LegalFundClassDetailsViewModel
    {
        Id = 101,
        Description = "Parent with child"
    },
    new LegalFundClassDetailsViewModel
    {
        Id = 102,
        Description = "Parent without child"
    },
    new LegalFundClassDetailsViewModel
    {
        Id = 103,
        Description = "I'm child",
        LegalParentClassId = 101
    }
};

var allTerms = (from fund in allFunds
                where fund.LegalParentClassId == null //only parents
                join child in allFunds on fund.Id equals child.LegalParentClassId into gj
                from child2 in gj.DefaultIfEmpty()
                select new LegalFundClassViewModel.LegalFundClassWrapper { LegalFundClassCommercialViewModel = fund, LegalFundClassSideLetterViewModel = child2 })
            .ToArray();