gpt4 book ai didi

c# - 从 Linq Group By 输出 2 个字段

转载 作者:行者123 更新时间:2023-11-30 14:20:27 25 4
gpt4 key购买 nike

我想按类上的 WorkGroup.GroupId 属性分组

public class Employee
{
public int EmployeeID {get; set;}
public Group WorkGroup {get; set;}
}

但是我需要输出组名属性(可以有重复但在同一个组 ID 之间是相同的)

类似的东西(这当然行不通):

var grps = from emp in Emps
group emp by emp.WorkGroup.GroupID into g
select new { GroupID = g.Key, Title = g.Key.WorkGroup.GroupTitle, Employees = g };

我正在寻找允许我做的输出:

foreach (var g in grps)
{
Console.WriteLine(g.Title + "-" + g.GroupID);
foreach (var e in g.Employees)
{
Console.WriteLine(e.EmployeeID);
}
}

最佳答案

只需按 ID 和标题分组:

var grps = 
from emp in Emps
group emp by new
{
GroupID = emp.WorkGroup.GroupID,
GroupTitle = emp.WorkGroup.GroupTitle
} into g
select new
{
GroupID = g.Key.GroupID,
GroupTitle = g.Key.GroupTitle,
Employees = g
};

关于c# - 从 Linq Group By 输出 2 个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1291855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com