简体   繁体   English

如何在列表中使用 Linq 搜索分层数据

[英]How to search Hierarchical Data with Linq in list

Distributor Registration.经销商注册。

I want to fill list with following information about the distributor我想在列表中填写有关经销商的以下信息

Id
ParentId
Name

For each distributor, it must be determined on whose recommendation the distributor is registered in the system.对于每个经销商,必须确定经销商在系统中注册的推荐人。 This can be anyone already registered in the system - in which case they must be selected from a list of already registered distributors, or the referrer information can be blank, which means that the distributor registers in the system without a recommender.这可以是已经在系统中注册的任何人——在这种情况下,他们必须从已注册的经销商列表中选择,或者推荐人信息可以是空白的,这意味着经销商在没有推荐人的情况下在系统中注册。 Each distributor can bring up to three people on its recommendation, the system should ensure that no more than three people are registered "under" the same distributor.每个经销商最多可以推荐三个人,系统应确保不超过三个人在同一经销商“下”注册。 Also, the depth of the hierarchy of the mentioned distributors should be maximum 5 - that is, the distributor can bring a person with a recommendation, who will also bring a person with his recommendation, etc. Maximum 5 levels.另外,上述经销商的层级深度应该最大为5级——也就是说,经销商可以带一个人推荐,他也会带一个人带他的推荐,等等。最多5个级别。 Accordingly, in such a group there can be a total of 1 + 3 + 9 + 27 + 81 = 121 people.因此,在这样的组中,总共可以有 1 + 3 + 9 + 27 + 81 = 121 人。 The system must provide control over the given levels.系统必须提供对给定级别的控制。

You can use recursion to find the depth of any element in the list and a plain old count to find the number of referrers.您可以使用递归来查找列表中任何元素的深度,并使用普通的旧计数来查找引用者的数量。

The following code is an implementation of that idea.以下代码是该想法的实现。

void Main()
{
    var distributors = new List<Distributor> {
        new Distributor { Id =  1, ParentId =  0, Name = "A" },
        new Distributor { Id =  7, ParentId =  1, Name = "B" },
        new Distributor { Id =  9, ParentId =  7, Name = "C" },
        new Distributor { Id = 13, ParentId =  9, Name = "D" },
        new Distributor { Id = 28, ParentId = 13, Name = "E" },
    };

    var valid = IsValidToAdd(distributors, 9);
    
    Console.WriteLine(valid);
}

public bool IsValidToAdd(List<Distributor> distributors, int parentId)
{
    var referCount = distributors.Count(d => d.ParentId == parentId);
    Console.WriteLine($"refer:{referCount}");
    if (referCount == 3)
    {
        Console.WriteLine("There are already 3 referals for this parent");
        return false;
    }

    var level = GetLevel(distributors, parentId);
    Console.WriteLine($"level: {level}");
    if (level > 5)
    {
        Console.WriteLine("There are already 5 levels of referals");
        return false;
    }
    
    return true;
}

public int GetLevel(List<Distributor> distributors, int parentId)
{
    var parent = distributors.FirstOrDefault(d => d.Id == parentId);

    if (parent == null)
    {
        return 1;
    }

    return 1 + GetLevel(distributors, parent.ParentId);
}

public class Distributor
{
    public int Id { get; set; }
    public int ParentId { get; set; }
    public string Name { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM