Search code examples
linqlinq-to-entities

How to get all child to specific levels like 4X15 level matrix


I have matrix like 4X15 and I want to find child up to 15 levels. Like if I send parent id 2 then it should return all the 4 records from level 1 and and up to next 15 levels

ID        Name        ParentId
1         A           0
2         B           1
3         C           1
4         D           2
5         E           2
6         F           1
7         G           1
8         H           3
9         I           4
10        J           5
11        K           3
.         .           .
.         .           .
100       XX          9
101       XA          10
 .         .           .
 .         .           .

So, for user 2 it should return all the records up to 15 levels. 2 => (4,5,9,10,100,101) Level1 (4,5) Level2(9), Level3(10), Level4(100), Level5(101)

Is there any way to find this in LINQ ?


Solution

  • I got its solution with LINQ and recursive function

    public List<MyNetworkModel> FetchUserNetworkData(int userId)
        {
            List<MyNetworkModel> userNetwork = new List<MyNetworkModel>();
            int counter = 1;
    
            List<MyNetworkModel> level1 = (from t in _ctx.Users
                                           where t.ParentId == userId
                                           select new MyNetworkModel
                                           {
                                               Id = t.Id,
                                               ParentId = t.ParentId,
                                               SponsorId = t.SponsorId,
                                               Fullname = t.Fullname,
                                               DateOfJoining = t.DateOfJoining,
                                               PhoneNumber = t.PhoneNumber,
                                               IsTccReceived = t.IsTccReceived ? "Yes" : "No",
                                               TccWalletAddress = t.TccWalletAddress,
                                               Rank = t.Rank,
                                               Level = 1
                                           }).ToList();
    
            AddUsers(ref userNetwork, level1, counter);
    
            return userNetwork;
        }
    
    private List<MyNetworkModel> AddUsers(ref List<MyNetworkModel> nestedUsers, List<MyNetworkModel> level, int counter)
        {
            foreach (var user in level)
            {
                nestedUsers.Add(user);
                GetChildren(user.Id, ref nestedUsers, counter);
            }
    
            return nestedUsers;
        }
    
    private void GetChildren(int userId, ref List<MyNetworkModel> nestedUsers, int counter)
        {
            counter++;
            List<MyNetworkModel> nthLevel = (from t in _ctx.Users
                                           where t.ParentId == userId
                                           select new MyNetworkModel
                                           {
                                               Id = t.Id,
                                               ParentId = t.ParentId,
                                               SponsorId = t.SponsorId,
                                               Fullname = t.Fullname,
                                               DateOfJoining = t.DateOfJoining,
                                               PhoneNumber = t.PhoneNumber,
                                               IsTccReceived = t.IsTccReceived ? "Yes" : "No",
                                               TccWalletAddress = t.TccWalletAddress,
                                               Rank = t.Rank,
                                               Level = counter
                                           }).ToList();
            if (nthLevel != null && nthLevel.Any() && counter <= 15)
            {
                AddUsers(ref nestedUsers, nthLevel, counter);
            }
        }
    

    Now just call FetchUserNetworkData(1); this will return data up to 15 levels with listing how many users are at what level.