问题描述
我要在报告上设置的对象
public class SurveyTemplateReportModel
{
public string Topic { get; set; }
public string Section { get; set; }
public string QuestionName { get; set; }
public string Question { get; set; }
public string Field { get; set; }
}
报告结构
**Topic 1**
**Section 1**
QuestionName 1.0
Question 1.0
Field 1.0
QuestionName 1.1
Question 1.1
Field 1.1
**Section 1.1**
QuestionName 1.1.0
Question 1.1.0
Field 1.1.0
QuestionName 1.1.1
Question 1.1.1
Field 1.1.1
**Section 2.0**
QuestionName 2.0.0
Question 2.0.0
Field 2.0.0
QuestionName 2.0.1
Question 2.0.1
Field 2.0.1
QuestionName 2.0.2
Question 2.0.2
Field 2.0.2
任何人都可以帮助我设置此类报告
解决方法
c#代码如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<SurveyTemplateReportModel> model = new List<SurveyTemplateReportModel>() {
new SurveyTemplateReportModel() { Topic = "1",Section = "1",QuestionName = "1.0",Question = "1.0",Field = "1.0"},new SurveyTemplateReportModel() { Topic = "1",QuestionName = "1.1",Question = "1.1",Field = "1.1"},Section = "1.1",QuestionName = "1.1.0",Question = "1.1.0",Field = "1.1.0"},QuestionName = "1.1.1",Question = "1.1.1",Field = "1.1.1"},Section = "2",QuestionName = "2.0.0",Question = "2.0.0",Field = "2.0.0"},QuestionName = "2.0.1",Question = "2.0.1",Field = "2.0.1"},QuestionName = "2.0.2",Question = "2.0.2",Field = "2.0.2"}
};
foreach (var topic in model.GroupBy(x => x.Topic))
{
Console.WriteLine("**Topic {0}**",topic.Key);
foreach (var section in topic.GroupBy(x => x.Section))
{
Console.WriteLine("{0}**Section {1}**",new string(' ',5),section.Key);
foreach (SurveyTemplateReportModel survey in section)
{
Console.WriteLine("{0}QuestionName = {1},Question = {2},Field = {3}",10),survey.QuestionName,survey.Question,survey.Field);
}
}
}
Console.ReadLine();
}
}
public class SurveyTemplateReportModel
{
public string Topic { get; set; }
public string Section { get; set; }
public string QuestionName { get; set; }
public string Question { get; set; }
public string Field { get; set; }
}
}