c# - 从列表中获取通用数据

标签 c# list linq group-by

<分区>

我在 C# 中有以下列表。

enter image description here

我需要生成以下数据。

  1. 在一行中,他们针对特定部门、个人资料和角色共有的所有用户和应用
  2. 在第 2 行中,每个用户都有一个唯一的应用程序,该应用程序未包含在第 1 行中。

下面是我需要的输出。

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", Role="B", Profile="C", User="X", Workstations="1", Applications="Teams" },
                new UserData {Department="A", Role="B", Profile="C", User="Y", Workstations="2", Applications="Skype" },
                new UserData {Department="A", Role="B", Profile="C", User="Y", Workstations="2", Applications="Teams" },
                new UserData {Department="A", Role="B", Profile="C", User="Z", Workstations="3", Applications="Skype" },
                new UserData {Department="A", Role="B", Profile="C", User="Z", Workstations="3", Applications="Teams" },
                new UserData {Department="A", Role="B", Profile="C", User="X", Workstations="1", Applications="Office" },
                new UserData {Department="A", Role="B", Profile="C", User="X", Workstations="1", Applications="PDF" },
                new UserData {Department="A", Role="B", Profile="C", User="Y", Workstations="2", Applications="Test" },
                new UserData {Department="D", Role="E", Profile="F", User="X", Workstations="1", Applications="Skype" },
                new UserData {Department="D", Role="E", Profile="F", User="X", Workstations="1", Applications="Teams" },
                new UserData {Department="D", Role="E", Profile="F", User="Y", Workstations="2", Applications="Skype" },
                new UserData {Department="D", Role="E", Profile="F", User="Y", Workstations="2", Applications="Teams" },
                new UserData {Department="D", Role="E", Profile="F", User="Z", Workstations="3", Applications="Skype" },
                new UserData {Department="D", Role="E", Profile="F", User="Z", Workstations="3", Applications="Teams" },
                new UserData {Department="D", Role="E", Profile="F", User="X", Workstations="1", Applications="Office" },
                new UserData {Department="D", Role="E", Profile="F", User="X", Workstations="1", Applications="PDF" },
                new UserData {Department="D", Role="E", Profile="F", User="Y", Workstations="2", 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, r.Department, r.Profile, r.Role, })
                                                .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.Profile,
                ac.Role,
                ac.User
            })
                   .Select(ac => new UserData
                   {
                       Department = ac.Key.Department,
                       Profile = ac.Key.Profile,
                       Role = ac.Key.Role,
                       User = ac.Key.User,
                       Applications = string.Join(",", 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, 
        g.Role, 
        g.Profile, 
        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();

关于c# - 从列表中获取通用数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64382941/

相关文章:

c# - 在 C# 中将基实例转换为派生类(向下转换)

c# - Flipview MVVM 绑定(bind) selecteditem 或 selectedIndex

c# - 常规使用 soap 错误的 WCF 客户端的处置

python - 多重排列,包括重复

c# - 如何将 Linq.ParallelQuery 转换为 Linq.IQueryable

c# - 接口(interface)和基类混合,正确的实现方式

javascript - 如何挂接到浏览器的上下文菜单事件

c++ - 列表 push_back C++ 期间未处理的异常

c# - 与 Linq 的不同日期

c# - SQL select where但只检查参数是否为真