如图,要得出
一年级
语文
课程同步
上册
下册
课外辅导
配套试卷
期中
期末
数学
课程同步
课外辅导
配套试卷
二年级
语文
不限方法,函数,循环,游标,存储过程都行。。。在线等啊!!!
为什么不放在逻辑中处理呢?哎,我白写一会的,~_~
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Data; 6 7 namespace ConsoleApplication2 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 DataTable dt = GetDt(); 14 IList<Entity> list = GetList(dt); 15 var query = from d in list 16 group d by d.ChannelName; 17 18 foreach (var v in query) 19 { 20 Console.WriteLine(v.Key); 21 22 var query1 = from d1 in v 23 group d1 by d1.ModuleName; 24 25 foreach (var v1 in query1) 26 { 27 Console.WriteLine("\t{0}", v1.Key); 28 29 var query2 = from d2 in v1 30 group d2 by d2.ProgrameNameOne; 31 32 foreach (var v2 in query2) 33 { 34 Console.WriteLine("\t\t{0}", v2.Key); 35 36 foreach (var end in v2) 37 { 38 if (!string.IsNullOrEmpty(end.ProgrameNameTwo)) 39 Console.WriteLine("\t\t\t{0}", end.ProgrameNameTwo); 40 } 41 } 42 } 43 } 44 45 } 46 47 /// <summary> 48 /// 获取list集合 49 /// </summary> 50 /// <param name="dt"></param> 51 /// <returns></returns> 52 private static IList<Entity> GetList(DataTable dt) 53 { 54 IList<Entity> list = new List<Entity>(); 55 foreach (DataRow dr in dt.Rows) 56 { 57 Entity model = new Entity(); 58 model.PKID = int.Parse(dr[0].ToString()); 59 model.ChannelName = dr[1].ToString(); 60 model.ModuleName = dr[2].ToString(); 61 model.ProgrameNameOne = dr[3].ToString(); 62 model.ProgrameNameTwo = dr[4].ToString(); 63 list.Add(model); 64 } 65 return list; 66 } 67 68 /// <summary> 69 /// 模拟数学源 70 /// </summary> 71 /// <returns></returns> 72 private static DataTable GetDt() 73 { 74 DataTable dt = new DataTable(); 75 dt.Columns.Add("PKID", typeof(int)); 76 dt.Columns.Add("ChannelName", typeof(string)); 77 dt.Columns.Add("ModuleName", typeof(string)); 78 dt.Columns.Add("ProgrameNameOne", typeof(string)); 79 dt.Columns.Add("ProgrameNameTwo", typeof(string)); 80 81 dt.Rows.Add(1, "一年级", "语文", "课程同步", "上册"); 82 dt.Rows.Add(2, "一年级", "语文", "课程同步", "下册"); 83 dt.Rows.Add(3, "一年级", "语文", "课外辅导", null); 84 dt.Rows.Add(4, "一年级", "语文", "配套试卷", "单元"); 85 dt.Rows.Add(5, "一年级", "语文", "配套试卷", "其中"); 86 dt.Rows.Add(6, "一年级", "语文", "配套试卷", "期末"); 87 dt.Rows.Add(7, "一年级", "数学", "课程同步", "上册"); 88 dt.Rows.Add(8, "一年级", "数学", "课程同步", "下册"); 89 dt.Rows.Add(9, "一年级", "数学", "课外辅导", null); 90 dt.Rows.Add(10, "一年级", "数学", "配套试卷", "单元"); 91 dt.Rows.Add(11, "一年级", "数学", "配套试卷", "其中"); 92 dt.Rows.Add(12, "一年级", "数学", "配套试卷", "期末"); 93 dt.Rows.Add(13, "二年级", "语文", "课程同步", "上册"); 94 dt.Rows.Add(14, "二年级", "语文", "课程同步", "下册"); 95 dt.Rows.Add(15, "二年级", "语文", "课外辅导", null); 96 dt.Rows.Add(16, "二年级", "语文", "配套试卷", "单元"); 97 dt.Rows.Add(17, "二年级", "语文", "配套试卷", "其中"); 98 dt.Rows.Add(18, "二年级", "语文", "配套试卷", "期末"); 99 return dt; 100 } 101 } 102 103 internal class Entity 104 { 105 public int PKID { get; set; } 106 public string ChannelName { get; set; } 107 public string ModuleName { get; set; } 108 public string ProgrameNameOne { get; set; } 109 public string ProgrameNameTwo { get; set; } 110 } 111 }
兄弟辛苦。。。但我前提是要用SQL语句实现
@LinkButton: 你是print打印出来,还是返回一个只有一列的表?
@xu_happy_you: 打印出来就行
想用一条语句解决是不可能的。用循环吧。