要获取每个部门的总结,可以使用ASP.NET中的LINQ查询。以下是一个示例代码,使用LINQ从数据库中获取每个部门的总结:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// 创建一个部门类
public class Department
{
public int DepartmentId { get; set; }
public string DepartmentName { get; set; }
}
// 创建一个总结类
public class Summary
{
public int DepartmentId { get; set; }
public string SummaryText { get; set; }
}
// 创建一个部门总结的列表
List summaries = new List()
{
new Summary { DepartmentId = 1, SummaryText = "Summary 1" },
new Summary { DepartmentId = 2, SummaryText = "Summary 2" },
new Summary { DepartmentId = 1, SummaryText = "Summary 3" },
new Summary { DepartmentId = 3, SummaryText = "Summary 4" }
};
// 创建一个部门列表
List departments = new List()
{
new Department { DepartmentId = 1, DepartmentName = "Department 1" },
new Department { DepartmentId = 2, DepartmentName = "Department 2" },
new Department { DepartmentId = 3, DepartmentName = "Department 3" }
};
// 使用LINQ查询每个部门的总结
var departmentSummaries = from summary in summaries
join department in departments on summary.DepartmentId equals department.DepartmentId
group summary by department into g
select new
{
DepartmentName = g.Key.DepartmentName,
Summaries = string.Join(", ", g.Select(s => s.SummaryText))
};
// 输出每个部门的总结
foreach (var departmentSummary in departmentSummaries)
{
Response.Write("Department: " + departmentSummary.DepartmentName + "
");
Response.Write("Summaries: " + departmentSummary.Summaries + "
");
}
}
}
在上面的示例中,我们首先定义了一个Department
类和一个Summary
类,分别表示部门和总结。然后,我们创建了一个summaries
列表,其中包含部门总结的数据,以及一个departments
列表,其中包含部门的数据。
接下来,我们使用LINQ查询将两个列表连接在一起,并按部门进行分组。最后,我们使用foreach
循环遍历每个部门的总结,并输出部门名称和总结内容。
请注意,上述示例仅用于演示如何使用LINQ从数据库中获取部门的总结,并输出到页面上。实际情况中,你可能需要根据你的环境和数据结构进行相应的修改和调整。