<分区>
我在 C# 中有以下列表。
我需要生成以下数据。
- 在一行中,他们针对特定部门、个人资料和角色共有的所有用户和应用
- 在第 2 行中,每个用户都有一个唯一的应用程序,该应用程序未包含在第 1 行中。
下面是我需要的输出。
请问有人能帮忙吗?
下面是详细信息。
类:
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 来避免循环?
提前致谢。