locked
Refactoring below code RRS feed

  • Question

  • User-1256377279 posted

    Hi Guys,

    Below is my Entity model which i use inheritance to refactor my code at certain extent

    using System;
    using System.Collections.Generic;
    using IamsAuthLib.Loaded.Models;
    
    namespace IamsAuthLib.Models
    {
    
        public class ATGenLoadFullView
        {
            public ATGenLoadFullView()
            {
                Repository = new List<MultiCol>();
                LevelOfDetail = new Dictionary<int, string>();
                LanguageCode = new List<MultiCol>();
                ScriptCode = new List<MultiCol>();          
                RelatedArchDescs = new List<MultiCol>();
                RelatedAuthFiles = new List<MultiCol>();
                ExternalIdentifier = new List<MultiCol>();
                NameAuthorityFunction = new List<MultiCol>();
                ATAFDescriptionRules = new Dictionary<int, string>();
                ATAFRelationshipNatureRules = new Dictionary<int, string>();
                ATAFRelationshipTypeRules = new Dictionary<int, string>();
                ATBiographyRefRule = new Dictionary<int, string>();
                ATCountriesRule = new Dictionary<int, string>();
                ATDateRule = new Dictionary<int, string>();
                ATNameRule = new Dictionary<int, string>();
                ATInstitutionIdentifierRule = new Dictionary<int, string>();
                ATLanguageRule = new Dictionary<int, string>();
                ATRelationshipNatureRule = new Dictionary<int, string>();
                ATRelationshipTypeRule = new Dictionary<int, string>();
                ATScriptRule = new Dictionary<int, string>();
                ATStructureRule = new Dictionary<int, string>();            
    
            }
    
            public string RecordId { get; set; }
            public int RowId { get; set; }
            public int ItemTypeId { get; set; }
            public string Level { get; set; }
            public int StatusId { get; set; }
            public string Structure { get; set; }
            public string CataloguerId { get; set; }
            public DateTime CataloguedDate { get; set; }
            public string ModifiedBy { get; set; }
            public DateTime? ModifiedDate { get; set; }
            public string ProcessedFile { get; set; }
            public string Reference { get; set; }
            public List<MultiCol> Repository { get; set; }     
            public Dictionary<int, string> LevelOfDetail { get; set; }
            public List<MultiCol> LanguageCode { get; set; }
            public List<MultiCol> ScriptCode { get; set; }       
            public string ItemType { get; set; }     
            public string History { get; set; }       
            public string RelatedResources { get; set; }
            public string Sources { get; set; }
            public string LOCCN { get; set; }
            public List<MultiCol> RelatedArchDescs { get; set; }
            public List<MultiCol> RelatedAuthFiles { get; set; }
            public List<MultiCol> ExternalIdentifier { get; set; }
            public List<MultiCol> NameAuthorityFunction { get; set; }       
    
            #region "Authority Rules"
            public Dictionary<int, string> ATAFDescriptionRules { get; set; }
            public Dictionary<int, string> ATAFRelationshipNatureRules { get; set; }
            public Dictionary<int, string> ATAFRelationshipTypeRules { get; set; }
            public Dictionary<int, string> ATBiographyRefRule { get; set; }
            public Dictionary<int, string> ATCountriesRule { get; set; }
            public Dictionary<int, string> ATDateRule { get; set; }
            public Dictionary<int, string> ATNameRule { get; set; }
            public Dictionary<int, string> ATInstitutionIdentifierRule { get; set; }
            public Dictionary<int, string> ATLanguageRule { get; set; }
            public Dictionary<int, string> ATRelationshipNatureRule { get; set; }
            public Dictionary<int, string> ATRelationshipTypeRule { get; set; }
            public Dictionary<int, string> ATScriptRule { get; set; }
            public Dictionary<int, string> ATStructureRule { get; set; }
            #endregion
        }
    
        public class ATCorpLoadFullView : ATGenLoadFullView
        {
            public ATCorpLoadFullView()
            {           
                CorpNameFull = new List<MultiCol>();
            }
    
            public List<MultiCol> CorpNameFull { get; set; }       
    
        }
    
        public class ATFamilyLoadFullView : ATGenLoadFullView
        {
            public ATFamilyLoadFullView()
            {
                FamilyNameFull = new List<MultiCol>();
            }
    
            public List<MultiCol> FamilyNameFull { get; set; }
    
        }
    
    
        public class ATPersonLoadFullView : ATGenLoadFullView
        {
            public ATPersonLoadFullView()
            {
                PersonNameFull = new List<MultiCol>();
            }
    
            public List<MultiCol> PersonNameFull { get; set; }
    
        }
    
    }
    

    But how to refactor below code as there is lot of repeatation 

    using System.Collections.Generic;
    using System.Linq;
    using IamsAuthLib.Models;
    using IamsAuthLib.Loaded.Models;
    
    namespace IamsAuthLib.Bal
    {
       public class ATGeneralBal
        {
    
            public List<ATCorpLoadFullView> GetFullCorpData(List<CorpLoad.CorpLoadTable> Tbl, List<CorpLoad.CorpLoadMandat> Mandat, List<CorpLoad.CorpLoadNonMandat> NonMandat)
            {
                var CorpList = (from t in Tbl
                                join m in Mandat on t.RowId equals m.RowId
                                join nm in NonMandat on t.RowId equals nm.RowId
                                select new ATCorpLoadFullView
                                {
                                    RecordId = t.RecordId,
                                    Structure = t.Structure,
                                    RowId = t.RowId,
                                    StatusId = t.StatusId,
                                    ProcessedFile = t.FileName,
                                    ItemTypeId = t.ItemTypeId,
                                    CataloguerId = t.CataloguerId,
                                    CataloguedDate = t.CataloguedDate,
                                    ModifiedBy = t.ModifiedBy,
                                    ModifiedDate = t.ModifiedDate,
    
                                    #region "Authority Rules"
                                    ATAFDescriptionRules = t.ATAFDescriptionRules,
                                    ATAFRelationshipNatureRules = t.ATAFRelationshipNatureRules,
                                    ATAFRelationshipTypeRules = t.ATAFRelationshipTypeRules,
                                    ATBiographyRefRule = t.ATBiographyRefRule,
                                    ATCountriesRule = t.ATCountriesRule,
                                    ATDateRule = t.ATDateRule,
                                    ATInstitutionIdentifierRule = t.ATInstitutionIdentifierRule,
                                    ATLanguageRule = t.ATLanguageRule,
                                    ATNameRule = t.ATNameRule,
                                    ATRelationshipNatureRule = t.ATRelationshipNatureRule,
                                    ATRelationshipTypeRule = t.ATRelationshipTypeRule,
                                    ATScriptRule = t.ATScriptRule,
                                    ATStructureRule = t.ATStructureRule,
                                    #endregion
    
    
    
                                    Repository = m.Repository,
                                    LevelOfDetail = m.LevelOfDetail,
                                    LanguageCode = m.LanguageCode,
                                    ScriptCode = m.ScriptCode,
                                    Level = m.Level,
                                    ItemType = m.ItemType,
                                    Reference = m.Reference,
                                    History = nm.History,
                                    NameAuthorityFunction = nm.NameAuthorityFunction,
                                    RelatedResources = nm.RelatedResources,
                                    Sources = nm.Sources,
                                    ExternalIdentifier = nm.ExternalIdentifier,
                                    LOCCN = nm.LOCCN,
                                    RelatedArchDescs = nm.RelatedArchDescs,
                                    RelatedAuthFiles =nm.RelatedAuthFiles,
                                    CorpNameFull = nm.CorpNameFull
                                    
                                                                    
    
                                }).ToList();
    
    
                return CorpList;
            }
    
            public List<ATFamilyLoadFullView> GetFullFamilyData(List<FamilyLoad.FamilyLoadTable> Tbl, List<FamilyLoad.FamilyLoadMandat> Mandat, List<FamilyLoad.FamilyLoadNonMandat> NonMandat)
            {
                var FamilyList = (from t in Tbl
                                join m in Mandat on t.RowId equals m.RowId
                                join nm in NonMandat on t.RowId equals nm.RowId
                                select new ATFamilyLoadFullView
                                {
                                    RecordId = t.RecordId,
                                    Structure = t.Structure,
                                    RowId = t.RowId,
                                    StatusId = t.StatusId,
                                    ProcessedFile = t.FileName,
                                    ItemTypeId = t.ItemTypeId,
                                    CataloguerId = t.CataloguerId,
                                    CataloguedDate = t.CataloguedDate,
                                    ModifiedBy = t.ModifiedBy,
                                    ModifiedDate = t.ModifiedDate,
    
                                    #region "Authority Rules"
                                    ATAFDescriptionRules = t.ATAFDescriptionRules,
                                    ATAFRelationshipNatureRules = t.ATAFRelationshipNatureRules,
                                    ATAFRelationshipTypeRules = t.ATAFRelationshipTypeRules,
                                    ATBiographyRefRule = t.ATBiographyRefRule,
                                    ATCountriesRule = t.ATCountriesRule,
                                    ATDateRule = t.ATDateRule,
                                    ATInstitutionIdentifierRule = t.ATInstitutionIdentifierRule,
                                    ATLanguageRule = t.ATLanguageRule,
                                    ATNameRule = t.ATNameRule,
                                    ATRelationshipNatureRule = t.ATRelationshipNatureRule,
                                    ATRelationshipTypeRule = t.ATRelationshipTypeRule,
                                    ATScriptRule = t.ATScriptRule,
                                    ATStructureRule = t.ATStructureRule,
                                    #endregion
    
    
                                    Repository = m.Repository,                               
                                    LevelOfDetail = m.LevelOfDetail,
                                    LanguageCode = m.LanguageCode,
                                    ScriptCode = m.ScriptCode,
                                    Level = m.Level,
                                    ItemType = m.ItemType,
                                    Reference = m.Reference,                              
                                    History = nm.History,
                                    NameAuthorityFunction = nm.NameAuthorityFunction,
                                    RelatedResources = nm.RelatedResources,
                                    Sources = nm.Sources,
                                    ExternalIdentifier = nm.ExternalIdentifier,
                                    LOCCN = nm.LOCCN,
                                    RelatedArchDescs = nm.RelatedArchDescs,
                                    RelatedAuthFiles = nm.RelatedAuthFiles,
                                    FamilyNameFull = nm.FamilyNameFull
                                }).ToList();
    
    
                return FamilyList;
            }
    
            public List<ATPersonLoadFullView> GetFullPersonData(List<PersonLoad.PersonLoadTable> Tbl, List<PersonLoad.PersonLoadMandat> Mandat, List<PersonLoad.PersonLoadNonMandat> NonMandat)
            {
                var PersonList = (from t in Tbl
                                  join m in Mandat on t.RowId equals m.RowId
                                  join nm in NonMandat on t.RowId equals nm.RowId
                                  select new ATPersonLoadFullView
                                  {
                                      RecordId = t.RecordId,
                                      Structure = t.Structure,
                                      RowId = t.RowId,
                                      StatusId = t.StatusId,
                                      ProcessedFile = t.FileName,
                                      ItemTypeId = t.ItemTypeId,
                                      CataloguerId = t.CataloguerId,
                                      CataloguedDate = t.CataloguedDate,
                                      ModifiedBy = t.ModifiedBy,
                                      ModifiedDate = t.ModifiedDate,
    
                                      #region "Authority Rules"
                                      ATAFDescriptionRules = t.ATAFDescriptionRules,
                                      ATAFRelationshipNatureRules = t.ATAFRelationshipNatureRules,
                                      ATAFRelationshipTypeRules = t.ATAFRelationshipTypeRules,
                                      ATBiographyRefRule = t.ATBiographyRefRule,
                                      ATCountriesRule = t.ATCountriesRule,
                                      ATDateRule = t.ATDateRule,
                                      ATInstitutionIdentifierRule = t.ATInstitutionIdentifierRule,
                                      ATLanguageRule = t.ATLanguageRule,
                                      ATNameRule = t.ATNameRule,
                                      ATRelationshipNatureRule = t.ATRelationshipNatureRule,
                                      ATRelationshipTypeRule = t.ATRelationshipTypeRule,
                                      ATScriptRule = t.ATScriptRule,
                                      ATStructureRule = t.ATStructureRule,
                                      #endregion
    
    
                                      Repository = m.Repository,
                                      LevelOfDetail = m.LevelOfDetail,
                                      LanguageCode = m.LanguageCode,
                                      ScriptCode = m.ScriptCode,
                                      Level = m.Level,
                                      ItemType = m.ItemType,
                                      Reference = m.Reference,
                                      History = nm.History,
                                      NameAuthorityFunction = nm.NameAuthorityFunction,
                                      RelatedResources = nm.RelatedResources,
                                      Sources = nm.Sources,
                                      ExternalIdentifier = nm.ExternalIdentifier,
                                      LOCCN = nm.LOCCN,
                                      RelatedArchDescs = nm.RelatedArchDescs,
                                      RelatedAuthFiles = nm.RelatedAuthFiles,
                                      PersonNameFull = nm.PersonNameFull
                                  }).ToList();
    
    
                return PersonList;
            }
    
    
    
        }
    }
    

    below  part is repeated in all 3 above methods

    RecordId = t.RecordId,
                                      Structure = t.Structure,
                                      RowId = t.RowId,
                                      StatusId = t.StatusId,
                                      ProcessedFile = t.FileName,
                                      ItemTypeId = t.ItemTypeId,
                                      CataloguerId = t.CataloguerId,
                                      CataloguedDate = t.CataloguedDate,
                                      ModifiedBy = t.ModifiedBy,
                                      ModifiedDate = t.ModifiedDate,
    
                                      #region "Authority Rules"
                                      ATAFDescriptionRules = t.ATAFDescriptionRules,
                                      ATAFRelationshipNatureRules = t.ATAFRelationshipNatureRules,
                                      ATAFRelationshipTypeRules = t.ATAFRelationshipTypeRules,
                                      ATBiographyRefRule = t.ATBiographyRefRule,
                                      ATCountriesRule = t.ATCountriesRule,
                                      ATDateRule = t.ATDateRule,
                                      ATInstitutionIdentifierRule = t.ATInstitutionIdentifierRule,
                                      ATLanguageRule = t.ATLanguageRule,
                                      ATNameRule = t.ATNameRule,
                                      ATRelationshipNatureRule = t.ATRelationshipNatureRule,
                                      ATRelationshipTypeRule = t.ATRelationshipTypeRule,
                                      ATScriptRule = t.ATScriptRule,
                                      ATStructureRule = t.ATStructureRule,
                                      #endregion
    
    
                                      Repository = m.Repository,
                                      LevelOfDetail = m.LevelOfDetail,
                                      LanguageCode = m.LanguageCode,
                                      ScriptCode = m.ScriptCode,
                                      Level = m.Level,
                                      ItemType = m.ItemType,
                                      Reference = m.Reference,
                                      History = nm.History,
                                      NameAuthorityFunction = nm.NameAuthorityFunction,
                                      RelatedResources = nm.RelatedResources,
                                      Sources = nm.Sources,
                                      ExternalIdentifier = nm.ExternalIdentifier,
                                      LOCCN = nm.LOCCN,
                                      RelatedArchDescs = nm.RelatedArchDescs,
                                      RelatedAuthFiles = nm.RelatedAuthFiles,

    Many thanks,

    Shabbir

    Tuesday, May 14, 2019 3:05 PM

Answers

  • User-893317190 posted

    Hi shabbir_215,

    I'm sorry ,I haven't thought this.

    What I could thought out for this is that  also selectiong Mandat and Nonmandat  into the list.

    Below is a simple sample.

    Assume I have two simple model.

     public class First
        {
            public int Id { get; set; }
            public string FirProp { get; set; }
        }
         public class Second
        {
            public int Id { get; set; }
            public string SecProp { get; set; }
        }

    I want to merge them into the model below.

     public class Third
        {
            public int Id { get; set; }
            public string FirProp { get; set; }
            public string SecProp { get; set; }
        }

    The I could to get all the property from Second and Third

       List<First> firsts = new List<First>
                {
                    new First{ FirProp ="prop1  first1", Id =1},
                    new First{ FirProp ="prop1  first1", Id =2},
                    new First{ FirProp ="prop1  first1", Id = 3}
                };
                List<Second> seconds = new List<Second>
                {
                    new Second { SecProp = " prop2 second1", Id=1},
                    new Second { SecProp = " prop2 second2", Id = 2},
                    new Second { SecProp = " prop2 second3", Id =3},
                };
    
                var query = (from t in firsts
                            join m in seconds
                            on t.Id equals m.Id 
                            select new { m,t}).ToList(); // select m and t
    
              var list = query.Select(n =>
                {
                    Third third = new Third();
                    foreach (PropertyInfo info in third.GetType().GetProperties())// loop through third's properties
                    {
    
                        PropertyInfo info2 = n.m.GetType().GetProperty(info.Name);  // get m's property
                        PropertyInfo info3 = n.t.GetType().GetProperty(info.Name); // get t's property
                        if (info2 != null)  // if info2  is not null ,set third's property to its value
                        {
                            object obj = info2.GetValue(n.m);
                            info.SetValue(third, obj);
                        }
                        if(info3 != null) if info3 is not null ,set third's property to its value
                        {
                            object obj = info3.GetValue(n.t);
                            info.SetValue(third, obj);
                        }
    
                    }
                    return third;
                });

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 16, 2019 1:46 AM
  • User-1256377279 posted

    Amazing Xu, It's perfect. I manage to refactor as per your amendments. It worked :) smile

      public List<ATPersonLoadFullView> GetFullPersonDb(List<PersonLoad.PersonLoadTable> Tbl, List<PersonLoad.PersonLoadMandat> Mandat, List<PersonLoad.PersonLoadNonMandat> NonMandat)
            {
    
                var query = (from t in Tbl
                             join m in Mandat on t.RowId equals m.RowId
                             join nm in NonMandat on t.RowId equals nm.RowId
                             select new { t, m, nm }).ToList();
    
                var PersonList = query.Select(k =>
                {
                    ATPersonLoadFullView ATPersonView = new ATPersonLoadFullView();
                    foreach (PropertyInfo info in ATPersonView.GetType().GetProperties())
                    {
                        PropertyInfo infot = k.t.GetType().GetProperty(info.Name);
                        PropertyInfo infom = k.m.GetType().GetProperty(info.Name);
                        PropertyInfo infonm = k.nm.GetType().GetProperty(info.Name);
                        if (infot != null)
                        {
                            object obj = infot.GetValue(k.t);
                            info.SetValue(ATPersonView, obj);
                        }
                        if (infom != null)
                        {
                            object obj = infom.GetValue(k.m);
                            info.SetValue(ATPersonView, obj);
                        }
                        if (infonm != null)
                        {
                            object obj = infonm.GetValue(k.nm);
                            info.SetValue(ATPersonView, obj);
                        }
    
    
                    }
                    return ATPersonView;
    
                }).ToList();
    
    
                return PersonList;
    
    
            }

    Great thanks,

    Shabbir

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 16, 2019 10:38 AM

All replies

  • User753101303 posted

    Hi,

    You could consider using a tool such as https://automapper.org/

    Tuesday, May 14, 2019 5:15 PM
  • User303363814 posted

    I would create a constructor on ATGenLoadFullView which takes three parameters.

    First parameter of type Table, second of type Mandat and third of type NonMandat

    The constructor would use the parameters to initialize the properties of the ATGenLoadFullView object.

    Queries would make use of the new constructor.

    In other words - the object should create itself from parameters rather than every piece of code being forced to do the contruction.

    Wednesday, May 15, 2019 1:28 AM
  • User-893317190 posted

    Hi shabbir_215,

    Since your final model's property name is similar to anonymous type , you could use reflection.

    My original model.

     [Table("First")]
        public partial class First
        {
            public int id { get; set; }
    
            [StringLength(50)]
            public string Info { get; set; }
    
            public decimal? amount { get; set; }
    
            [StringLength(100)]
            public string catname { get; set; }
    
            [StringLength(100)]
            public string desc { get; set; }
        }

    My target model. They have the same property name.

     public partial class First1
        {
            public int id { get; set; }
    
         
            public string info { get; set; }
    
            public decimal? amount { get; set; }
    
          
            public string catname { get; set; }
    
          
            public string desc { get; set; }
        }

    And my test code.

      Model1DbContext dbContext = new Model1DbContext();
                var models = dbContext.Firsts.Select(m =>
                     new
                     {
                         id = m.id,
                         info = m.Info,
                         amount = m.amount,
                         catname = m.catname
                     }
    
                  ).ToList().Select(k =>
                  {
                       First1 first1 = new First1();
    foreach (PropertyInfo info in first1.GetType().GetProperties()) // get all the properties of first1 and loop through the properties
    {

    PropertyInfo info2 = k.GetType().GetProperty(info.Name); // get property of k with the same property name
    if (info2 != null)
    {
    object obj = info2.GetValue(k); // get the value of the property
    info.SetValue(first1, obj); // set value to first1's property
    }

    }
    return first1; }).ToList(); List<First1> first1s = models;

    The result.

    Best reflection will cause a little performance problem , please make your own choice.

    Best regards,

    Ackerly Xu

    Wednesday, May 15, 2019 8:12 AM
  • User-1256377279 posted

    Thanks XU,

    But in my First1 or Second model there is extra property which only specific to First1 and Second model and it doesn't exist in Orginal model

    Person Model

      PersonNameFull = nm.PersonNameFull

    Corp Model

    CorpNameFull = nm.CorpNameFull

    Family Model 

    FamilyNameFull = nm.FamilyNameFull

    Wednesday, May 15, 2019 8:30 AM
  • User-893317190 posted

    Hi shabbir_215 ,

    Not sure whether I have understood what you meant.

    My original model means the anonymous type.

    For example,

    var PersonList = (from t in Tbl
                                  join m in Mandat on t.RowId equals m.RowId
                                  join nm in NonMandat on t.RowId equals nm.RowId
                                  select t
                                  ).ToList().Select(k =>
    {
    ATPersonLoadFullView first1 = new ATPersonLoadFullView();
    foreach (PropertyInfo info in first1.GetType().GetProperties())
    {

    PropertyInfo info2 = k.GetType().GetProperty(info.Name);
    if (info2 != null)
    {
    object obj = info2.GetValue(k);
    info.SetValue(first1, obj);
    }

    }
    return first1;
    }).ToList(); return PersonList;

    Then you could get all the value from t.

    Best regards,

    Ackerly Xu

    Wednesday, May 15, 2019 9:42 AM
  • User-1256377279 posted

    Thanks Xu, you're genius.

    I exactly wanted like this way, but it sets on Table (t) value to First1 i want all Mandat and Nonmandat value should assign as well to First 1.

    Hope it make sense.

    Many Thanks,

    Shabbir

    Wednesday, May 15, 2019 2:33 PM
  • User-893317190 posted

    Hi shabbir_215,

    I'm sorry ,I haven't thought this.

    What I could thought out for this is that  also selectiong Mandat and Nonmandat  into the list.

    Below is a simple sample.

    Assume I have two simple model.

     public class First
        {
            public int Id { get; set; }
            public string FirProp { get; set; }
        }
         public class Second
        {
            public int Id { get; set; }
            public string SecProp { get; set; }
        }

    I want to merge them into the model below.

     public class Third
        {
            public int Id { get; set; }
            public string FirProp { get; set; }
            public string SecProp { get; set; }
        }

    The I could to get all the property from Second and Third

       List<First> firsts = new List<First>
                {
                    new First{ FirProp ="prop1  first1", Id =1},
                    new First{ FirProp ="prop1  first1", Id =2},
                    new First{ FirProp ="prop1  first1", Id = 3}
                };
                List<Second> seconds = new List<Second>
                {
                    new Second { SecProp = " prop2 second1", Id=1},
                    new Second { SecProp = " prop2 second2", Id = 2},
                    new Second { SecProp = " prop2 second3", Id =3},
                };
    
                var query = (from t in firsts
                            join m in seconds
                            on t.Id equals m.Id 
                            select new { m,t}).ToList(); // select m and t
    
              var list = query.Select(n =>
                {
                    Third third = new Third();
                    foreach (PropertyInfo info in third.GetType().GetProperties())// loop through third's properties
                    {
    
                        PropertyInfo info2 = n.m.GetType().GetProperty(info.Name);  // get m's property
                        PropertyInfo info3 = n.t.GetType().GetProperty(info.Name); // get t's property
                        if (info2 != null)  // if info2  is not null ,set third's property to its value
                        {
                            object obj = info2.GetValue(n.m);
                            info.SetValue(third, obj);
                        }
                        if(info3 != null) if info3 is not null ,set third's property to its value
                        {
                            object obj = info3.GetValue(n.t);
                            info.SetValue(third, obj);
                        }
    
                    }
                    return third;
                });

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 16, 2019 1:46 AM
  • User-1256377279 posted

    Amazing Xu, It's perfect. I manage to refactor as per your amendments. It worked :) smile

      public List<ATPersonLoadFullView> GetFullPersonDb(List<PersonLoad.PersonLoadTable> Tbl, List<PersonLoad.PersonLoadMandat> Mandat, List<PersonLoad.PersonLoadNonMandat> NonMandat)
            {
    
                var query = (from t in Tbl
                             join m in Mandat on t.RowId equals m.RowId
                             join nm in NonMandat on t.RowId equals nm.RowId
                             select new { t, m, nm }).ToList();
    
                var PersonList = query.Select(k =>
                {
                    ATPersonLoadFullView ATPersonView = new ATPersonLoadFullView();
                    foreach (PropertyInfo info in ATPersonView.GetType().GetProperties())
                    {
                        PropertyInfo infot = k.t.GetType().GetProperty(info.Name);
                        PropertyInfo infom = k.m.GetType().GetProperty(info.Name);
                        PropertyInfo infonm = k.nm.GetType().GetProperty(info.Name);
                        if (infot != null)
                        {
                            object obj = infot.GetValue(k.t);
                            info.SetValue(ATPersonView, obj);
                        }
                        if (infom != null)
                        {
                            object obj = infom.GetValue(k.m);
                            info.SetValue(ATPersonView, obj);
                        }
                        if (infonm != null)
                        {
                            object obj = infonm.GetValue(k.nm);
                            info.SetValue(ATPersonView, obj);
                        }
    
    
                    }
                    return ATPersonView;
    
                }).ToList();
    
    
                return PersonList;
    
    
            }

    Great thanks,

    Shabbir

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 16, 2019 10:38 AM