Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

Efficient way to be update grand child records in c# using LINQ

$
0
0

I have to update a column 'PPMOProjectID' in the table, but in such way that if it is empty and tricky part is

I have a scope of only parent child relation ship in the table like if MP- master parent, P- parent, Child - C:

    ID   ParentID PPMOProjectID
    MP1     NULL     NULL //updating here by 889
    P1      MP1      898 //It should not update, because it contains already value
    P2      MP1      NULL//it should update by 889 if it is empty else skip it
    C1       P1      NULL//Here it should update by 898
    C2       P1      NULL//Here it should update by 898
    C3       P2      NULL//Here it should update by 889

As per above logic :When I updating PPMOProjectID in parent record, I have to check all children's and master children's , if the record is empty I have to update by Immediate record parent, it that parent is empty and need to update by master column ID, if parent record has column ID , should update by Parent ID itself.

This should be done in C# and LINQ , I have tried by below logic :

    var childProjects = db.Projects.Where(m => m.AC_ParentProject_ID == AC_ProjectID).ToList();
    var updateRecords = new List<Project>();
     while (childProjects.Count == 0)
     {

       foreach(var item in childProjects)
        {
          updateRecords = childProjects.Where(m => m.AC_ParentProject_ID == item.AC_ParentProject_ID).ToList();
          //saving logic here..
        }
      }
But how to get grand childrens it should keep on checking.

I am struggling to find a way for above requirement. can anyone please help on this.


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>