Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
以分层方式获取嵌套元素C#linq_C#_Linq - Fatal编程技术网

以分层方式获取嵌套元素C#linq

以分层方式获取嵌套元素C#linq,c#,linq,C#,Linq,我有一个对象列表,它映射为父子关系。 所以父母有很多孩子,孙子孙女等等。 我已将表格组织如下 UserId UserNme 1 Jack 2 William 3 asha 4 winston ParentId ChildId 2 3 3 4 因此我想以以下分层方式列出用户: public List<MarketingUserDto> GetChildAgents(List&l

我有一个对象列表,它映射为父子关系。 所以父母有很多孩子,孙子孙女等等。 我已将表格组织如下

UserId UserNme 
1      Jack    
2      William   
3      asha      
4      winston

ParentId ChildId
2          3
3          4
因此我想以以下分层方式列出用户:

 public List<MarketingUserDto> GetChildAgents(List<MarketingUserDto> agents,List<MarketingUserDto> resultAgents)
    {

        if (agents.Count == 0)
        {                
            var parentagents = _userRegistrationRepository.GetMany(x => ((x.IsParentAgent ?? false) == true && x.UserTypeId == (short)Enums.UserTypes.Agent) || (x.UserTypeId == (short)Enums.UserTypes.Super_Manager && (x.IsParentAgent ?? false) == false));
            this.GetChildAgents(Mapper.Map<List<UserRegistration>, List<MarketingUserDto>>(parentagents.ToList()), resultAgents);
        }
        else
        {
            foreach (var agent in agents)
            {
                var childagents = _agentMappingRepository.GetMany(x => x.ParentId == agent.UserId, y => y.UserRegistration);

            }
        }
        return resultAgents;
    }
->非子用户(父/非父)
---->子用户
-------->孙子用户
----------->伟大的孙儿用户

我尝试了以下代码,但没有完成:

 public List<MarketingUserDto> GetChildAgents(List<MarketingUserDto> agents,List<MarketingUserDto> resultAgents)
    {

        if (agents.Count == 0)
        {                
            var parentagents = _userRegistrationRepository.GetMany(x => ((x.IsParentAgent ?? false) == true && x.UserTypeId == (short)Enums.UserTypes.Agent) || (x.UserTypeId == (short)Enums.UserTypes.Super_Manager && (x.IsParentAgent ?? false) == false));
            this.GetChildAgents(Mapper.Map<List<UserRegistration>, List<MarketingUserDto>>(parentagents.ToList()), resultAgents);
        }
        else
        {
            foreach (var agent in agents)
            {
                var childagents = _agentMappingRepository.GetMany(x => x.ParentId == agent.UserId, y => y.UserRegistration);

            }
        }
        return resultAgents;
    }
public List GetChildAgents(列表代理、列表结果)
{
如果(agents.Count==0)
{                
var parentagents=\u userRegistrationRepository.GetMany(x=>((x.IsParentAgent??false)==true和&x.UserTypeId==(short)Enums.UserTypes.Agent)|(x.UserTypeId==(short)Enums.UserTypes.Super\u Manager和(x.IsParentAgent??false)==false));
this.GetChildAgents(Mapper.Map(parentagents.ToList()),resultAgents);
}
其他的
{
foreach(代理中的var代理)
{
var childagents=\u agentmappingpository.GetMany(x=>x.ParentId==agent.UserId,y=>y.UserRegistration);
}
}
返回结果;
}

您能提供一些建议吗?

如果您不想重新发明展平轮,请看Morelink的
展平()


如果你抓住了,你会得到它和更多有用的LINQ扩展。

如果你不想重新发明扁平化轮子,看看MoreLinq的
扁平化()


如果你抓住了,你会得到它和更多有用的LINQ扩展。

那么你想让它在一个单数列表中展平吗?你不能在更靠近数据源的地方展平吗?你想让它在一个单数列表中展平吗?你不能在更靠近数据源的地方展平吗?