通过LINQ

时间:2019-07-17 12:22:50

标签: c# linq

我有这个来源,可以从使用linq的方法中使用的过程中获得。我必须按ProfileParent对源进行分组,然后按ProfileSubParent和ProfileName作为节点。我的问题是,并非所有ProfileName都有一个ProfileSubParent,并且发生这种情况时,我不希望将它们分组,而只是希望它们独立存在。因此,在没有ProfileSubParent的情况下,我希望从三级树中选择两级树

Id ProfileName ProfileParent ProfileSubParent Active 1 Node 1 Menu Sub menu 1 1
2 Node 2 Menu Sub menu 1 1 3 Node 3 Menu - 1 4 Node 4 Menu - 1

我想从使用linq的方法中获得的回报是:

[{
    "label":"Menu",
    "selected":false,
    "__ivhTreeviewIndeterminate":false,
    "children":[  
    {  
        "label":"Sub menu 1",
        "selected":false,
        "__ivhTreeviewIndeterminate":false,
        "children":[  
        {  
            "Id":1,
            "ProfileName":"Node 1",
            "ProfileDescription":"Node 1",
            "ProfileLongDescription":"Node 1",
            "Active":false,
            "label":"Node 1",
            "selected":false
        },
        {  
            "Id":2,
            "ProfileName":"Node 2",
            "ProfileDescription":"Node 2",
            "ProfileLongDescription":"Node 2",
            "Active":false,
            "label":"Node 2",
            "selected":false
        }
    ]
    },
    {
        "label":"Node 3",
        "selected":false,
        "__ivhTreeviewIndeterminate":false,
        "children":[]
    },
    {
        "label":"Node 4",
        "selected":false,
        "__ivhTreeviewIndeterminate":false,
        "children":[]
    }]
}]

这是我的代码,但是从中获得的回报不是我想要的。

 result.GroupBy(gr => new { gr.ProfileParent })
                      .Select(grupet =>  new ProfileRightsParent
                      {
                          label = grupet.Key.ProfileParent,
                          selected = grupet.Count() == grupet.Where(x => x.Active == true).ToList().Count() ? true : false,
                          __ivhTreeviewIndeterminate = (grupet.Count() != grupet.Where(x => x.Active == true).ToList().Count() && grupet.Where(x => x.Active == true).ToList().Count() != 0) ? true : false,
                          children = grupet.GroupBy(sub => new { sub.ProfileSubParent }).Select
                                      (sub => new ProfileRightsSubParent
                                      {
                                          label = sub.Key.ProfileSubParent,
                                          selected = sub.Count() == sub.Where(x => x.Active == true).ToList().Count() ? true : false,
                                          __ivhTreeviewIndeterminate = (sub.Count() != sub.Where(x => x.Active == true).ToList().Count() && sub.Where(x => x.Active == true).ToList().Count() != 0) ? true : false,
                                          children = sub.Select(details => new ProfileLine()
                                          {
                                              Id = details.Id,
                                              ProfileName = details.ProfileName,
                                              ProfileDescription = details.ProfileDescription,
                                              ProfileLongDescription = details.ProfileLongDescription,
                                              Active = details.Active,
                                              selected = details.Active,
                                              label = details.ProfileDescription
                                          }).ToList()

                                      }).ToList()
                      }).ToList();

我得到的回报是:

[{  
    "label":"Menu",
    "selected":false,
    "__ivhTreeviewIndeterminate":false,
    "children":[  
    {  
        "label":"Sub menu 1",
        "selected":false,
        "__ivhTreeviewIndeterminate":false,
        "children":[  
        {  
            "Id":1,
            "ProfileName":"Node 1",
            "ProfileDescription":"Node 1",
            "ProfileLongDescription":"Node 1",
            "Active":false,
            "label":"Node 1",
            "selected":false
        },
        {  
            "Id":2,
            "ProfileName":"Node 2",
            "ProfileDescription":"Node 2",
            "ProfileLongDescription":"Node 2",
            "Active":false,
            "label":"Node 2",
            "selected":false
        }]
    },
    {  
        "label":" ",
        "selected":false,
        "__ivhTreeviewIndeterminate":false,
        "children":[  
        {  
            "Id":1,
            "ProfileName":"Node 3",
            "ProfileDescription":"Node 3",
            "ProfileLongDescription":"Node 3",
            "Active":false,
            "label":"Node 3",
            "selected":false
        },
        {  
            "Id":2,
            "ProfileName":"Node 4",
            "ProfileDescription":"Node 4",
            "ProfileLongDescription":"Node 4",
            "Active":false,
            "label":"Node 4",
            "selected":false
        }]
    },]
}]

1 个答案:

答案 0 :(得分:0)

您可以在执行Where()之前先执行GroupBy,以避免使用空元素。 我发现的最简单的方法是在全局AddRange之后添加带有GroupBy的元素。 我还使用AllAny方法简化了Linq的某些元素

var res = result.GroupBy(gr => gr.ProfileParent)
         .Select(grupet => new ProfileRightsParent
         {
             label = grupet.Key,
             selected = grupet.All(x => x.Active),
             __ivhTreeviewIndeterminate = !grupet.All(x => x.Active) && grupet.Any(x => x.Active),
             children = grupet.Where(x => !string.IsNullOrEmpty(x.ProfileSubParent)).GroupBy(sub => sub.ProfileSubParent).Select
                         (sub => new ProfileRightsSubParent
                         {
                             label = sub.Key,
                             selected = sub.All(x => x.Active),
                             __ivhTreeviewIndeterminate = !sub.All(x => x.Active) && sub.Any(x => x.Active),
                             children = sub.Select(details => new ProfileLine()
                             {
                                 Id = details.Id,
                                 ProfileName = details.ProfileName,
                                 ProfileDescription = details.ProfileDescription,
                                 ProfileLongDescription = details.ProfileLongDescription,
                                 Active = details.Active,
                                 selected = details.Active,
                                 label = details.ProfileDescription
                             }).ToList()
                         }).ToList()
         }).ToList();

res.AddRange(result
    .Where(x => string.IsNullOrEmpty(x.ProfileSubParent))
    .Select(detail => new ProfileRightsParent
    {
        label = detail.ProfileName,
        selected = false,
        __ivhTreeviewIndeterminate = false
    }));