微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从列表中获取通用数据

如何解决从列表中获取通用数据

我在C#中具有以下列表。

enter image description here

我需要生成以下数据。

  1. 在一排中,他们在特定部门,个人资料和角色上拥有的所有共同用户和应用
  2. 在第二行中,每个用户都有一个唯一的应用程序,该应用程序不包含在第一行中。

下面是我需要的输出

enter image description here

请有人在这里提供帮助吗?

下面是详细信息。

班级

class UserData
{
    public string Department { get; set; }
    public string Role { get; set; }
    public string Profile { get; set; }
    public string User { get; set; }
    public string Workstations { get; set; }
    public string Applications { get; set; }
}

列表:

List<UserData> tempList = new List<UserData>
            {
                new UserData {Department="A",Role="B",Profile="C",User="X",Workstations="1",Applications="Skype" },new UserData {Department="A",Applications="Teams" },User="Y",Workstations="2",User="Z",Workstations="3",Applications="Office" },Applications="PDF" },Applications="Test" },new UserData {Department="D",Role="E",Profile="F",Applications="Test" }
            };

这是我的解决方案:

List<UserData> CommonList = new List<UserData>();
List<UserData> UnCommonList = new List<UserData>();

   var UniqueUsers = tempList.GroupBy(r => new { r.User,r.Department,r.Profile,r.Role,})
                                                .OrderBy(g => g.Key.User).distinct().ToList();

            var UniqueApplications = tempList.GroupBy(r => new { r.Applications,})
                                                .OrderBy(g => g.Key.Applications).ToList();

            foreach (var app in UniqueApplications)
            {
                var usersForApp = tempList.Where(x => x.Applications.Equals(app.Key.Applications) && x.Department.Equals(app.Key.Department) && x.Profile.Equals(app.Key.Profile) && x.Role.Equals(app.Key.Role)).distinct().ToList();
                var usersGroup = UniqueUsers.GroupBy(r => new { r.Key.User,r.Key.Department,r.Key.Profile,r.Key.Role,})
                                                        .Where(x => x.Key.Department.Equals(app.Key.Department) && x.Key.Profile.Equals(app.Key.Profile) && x.Key.Role.Equals(app.Key.Role))
                                                .OrderBy(g => g.Key.User).distinct().ToList();

                if (usersForApp != null)
                {
                    if(usersForApp.Count() == usersGroup.Count())
                    {
                        CommonList.AddRange(usersForApp);
                    }
                    else
                    {
                        UnCommonList.AddRange(usersForApp);

                    }
                }
            }
            var FinalCommon = CommonList.GroupBy(ac => new
            {
                ac.Department,ac.Profile,ac.Role
            })
                    .Select(ac => new UserData
                    {
                        Department = ac.Key.Department,Profile = ac.Key.Profile,Role = ac.Key.Role,User = string.Join(",",ac.Select(y => y.User).ToList().distinct().ToArray()),Applications = string.Join(",ac.Select(y => y.Applications).distinct().ToList().ToArray())
                    }).ToList();

            var FinalUnCommon = UnCommonList.GroupBy(ac => new
            {
                ac.Department,ac.Role,ac.User
            })
                   .Select(ac => new UserData
                   {
                       Department = ac.Key.Department,User = ac.Key.User,ac.Select(y => y.Applications).distinct().ToList().ToArray())
                   }).ToList();

有人可以帮助我使用Linq来避免循环吗?

预先感谢。

解决方法

这是我的尝试。但是,这不是一个LINQ语句。

// find out what applications are common
var firstPass = tempList.GroupBy(g => new
{
    g.Department,g.Role,g.Profile,g.User,g.Workstations
}).Select(g => new
{
    g.Key,Applications = g.Select(o => o.Applications)
});

var distinctGroupsCount = firstPass.Count();

// assume if an application repeats this amount it is available everywhere

var appsEveryoneHas = tempList.GroupBy(s => s.Applications)
    .Where(g => g.Count() == distinctGroupsCount)
    .Select(s => s.Key)
    .ToHashSet();
  

var finalResult = tempList
    .GroupBy(g => new
    {
        g.Department,User = appsEveryoneHas.Contains(g.Applications) ? string.Empty : g.User
    }).
    Select(s => new UserData
    {
        Department = s.First().Department,Role = s.First().Role,Profile = s.First().Profile,Workstations = string.Join(',',s.Select(o => o.Workstations).Distinct()),User = string.Join(',s.Select(o => o.User).Distinct()),Applications = string.Join(',s.Select(o => o.Applications).Distinct())
    })
    .ToList();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。