按已知父母订购物品

时间:2016-03-25 19:16:24

标签: c# linq

我们说我有一个叫LinkedUser的课程,看起来像这样:

public class LinkedUser 
{
     public int Id {get; set;}         
     public int? ParentId {get; set;}
     public string CardNumber {get; set;}
     public int RootUserId {get; set;}
}

现在,让我们说我想要检索LinkedUser所有的RootUserId == 1,然后我找回看起来像这样的数据,没有特别的顺序:

var linkedUserList = return new List<LinkedUser>()
{
    new LinkedUser {Id = 1, CardNumber = "546251357655", ParentId = null, RootUserId = 1},
    new LinkedUser {Id = 5869, CardNumber = "666547395503", ParentId = 1, RootUserId = 1},
    new LinkedUser {Id = 214, CardNumber = "666558432178", ParentId = 5869, RootUserId = 1},
    new LinkedUser {Id = 8957, CardNumber = "987265985430", ParentId = 214, RootUserId = 1},
    new LinkedUser {Id = 3650, CardNumber = "987653215430", ParentId = 8957, RootUserId = 1}
};

我的问题:

我想要做的是排序这个列表,在继承方面降序ParentId,而不仅仅是原始整数值。

我首先通过在类中添加index属性来解决这个问题,但我觉得这可以通过LINQ更优雅地完成 - 我只是无法绕过语法来完成它

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

是否可以构建一个真正的树?

var linkedUserList = return new List<LinkedUser>()
{
    new LinkedUser {Id = 1, CardNumber = "546251357655", ParentId = null, RootUserId = 1, Children = new [] {
        new LinkedUser {Id = 5869, CardNumber = "666547395503", ParentId = 1, RootUserId = 1, Children = new [] {
            new LinkedUser {Id = 214, CardNumber = "666558432178", ParentId = 5869, RootUserId = 1},
            ... etc
        }},

    }},
};

这使您可以单独操作每个子集合。

所以现在你可以走树了(未编译);

public void SortUser(LinkedUser user)
{
    user.Children.Sort((c1,c2) => Math.sign(c2-c1));
    foreach(var child in user.Children)
    {
        SortUser(child);
    }
}

在LinkedUser上粘贴'后代'属性;

public IEnumrable<LinkedUser> Descendents()
{
    yield return this;
    foreach(var descendent in Children)
    {
        foreach(var result in child.Descendents())
        {
            yield return descendent;
        }
    }
}

现在你可以做;

var hierarchicallyOrdered = linkedUserList.SelectMany(usr => usr.Descendents()).ToList();

答案 1 :(得分:0)

public class LinkedUser
{
    public int Id { get; set; }
    public int? ParentId { get; set; }
    public string CardNumber { get; set; }
    public int RootUserId { get; set; }
}

var linkedUserList =  new List<LinkedUser>()
{
    new LinkedUser {Id = 1, CardNumber = "546251357655", ParentId = null, RootUserId = 1},
    new LinkedUser {Id = 5869, CardNumber = "666547395503", ParentId = 1, RootUserId = 1},
    new LinkedUser {Id = 214, CardNumber = "666558432178", ParentId = 5869, RootUserId = 1},
    new LinkedUser {Id = 8957, CardNumber = "987265985430", ParentId = 214, RootUserId = 1},
    new LinkedUser {Id = 3650, CardNumber = "987653215430", ParentId = 8957, RootUserId = 1}
};

List<LinkedUser> sortedList = linkedUserList.OrderBy(o => o.ParentId).ToList();

使用调试器检查linkedUserList您将从Items[0] thru item[4]看到sortedList变量

  

item [0] ParentId = null

     

item [1] ParentId = 1

     

item [2] ParentId = 214

     

item [3] ParentId = 5869

     

item [4] ParentId = 8957