下面的循环如何写 本帖最后由 caozhy 于 2011-06-14 21:41:36 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 3张表就够了,类别、品牌、型号。品牌表里有一个字段表示类别id,型号表里有一个字段表示品牌id,应该够了 using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Text;namespace ConsoleApplication1{ class Program { class Data { public int Level { get; set; } public string ID { get; set; } public string Name { get; set; } public List<Data> Childs { get; set; } public Data() { Childs = new List<Data>(); } public Data this[string id] { get { if (ID == id) return this; Data data = null; if (Childs.Count > 0) { data = Childs.Where(x => x.ID == id).SingleOrDefault(); if (data == null) { foreach (var item in Childs) { data = item[id]; if (data != null) break; } } } return data; } } public override string ToString() { string TableChar = ""; for (int i = 0; i < Level; i++) TableChar += "\t"; StringBuilder sb = new StringBuilder(); if (Childs.Count == 0) { sb.Append(string.Format("{0}{{name:{1}}}", TableChar, Name)); } else { sb.AppendLine(string.Format("{0}{{name:{1}[", TableChar, Name)); for (int i = 0; i < Childs.Count; i++) { sb.AppendLine("\t" + Childs[i].ToString() + ((i == Childs.Count - 1) ? "" : ",")); } sb.Append(string.Format("\t{0}]}}", TableChar)); } return sb.ToString(); } } static void Main(string[] args) { DataTable dt = new DataTable(); dt.Columns.Add("id"); dt.Columns.Add("name"); dt.Rows.Add("01", "电脑"); dt.Rows.Add("02", "手机"); dt.Rows.Add("03", "电视机"); dt.Rows.Add("0101", "联想"); dt.Rows.Add("0102", "华硕"); dt.Rows.Add("0103", "惠普"); dt.Rows.Add("0201", "诺基亚"); dt.Rows.Add("0202", "三星"); dt.Rows.Add("0301", "夏普"); dt.Rows.Add("0302", "海尔"); dt.Rows.Add("020101", "N81"); dt.Rows.Add("020102", "N95"); var list = (from x in dt.Rows.Cast<DataRow>() group x by x["id"].ToString().Length into g select new { Key = g.Key, Items = g.ToList() }).OrderBy(x => x.Key); List<Data> DataRoot = new List<Data>(); list.ToList() .ForEach(x => { if (x.Key == 2) { x.Items.ForEach(y => DataRoot.Add(new Data() { ID = y["id"].ToString(), Name = y["Name"].ToString(), Level = x.Key / 2 - 1 })); } else { x.Items.ForEach(y => { Data data = null; foreach (var item in DataRoot) { data = item[y["id"].ToString().Substring(0, y["id"].ToString().Length - 2)]; if (data != null) break; } if (data != null) { data.Childs.Add(new Data() { ID = y["id"].ToString(), Name = y["Name"].ToString(), Level = x.Key / 2 - 1 }); } }); } }); StringBuilder sb = new StringBuilder(); for (int i = 0; i < DataRoot.Count; i++) { sb.AppendLine(DataRoot[i].ToString()); } string Result = sb.ToString(); Console.WriteLine(Result); } }}{name:电脑[ {name:联想}, {name:华硕}, {name:惠普} ]}{name:手机[ {name:诺基亚[ {name:N81}, {name:N95} ]}, {name:三星} ]}{name:电视机[ {name:夏普}, {name:海尔} ]}Press any key to continue . . . 太精彩了,收藏并研究。我一开始的思路是这样,类似数据库表操作,自己Join自己如dt join dt按照条件 d2=>d2.Id.StartWith(d1.Id) && d2.Id != d1.Id将结果存到一个新的dt中,列为 Id1 Id2 (Id2开头部分为Id1),再用新的dt2 jion dt得到dt3(Id,Id2,Id3)继续join直到Idn的值全部为null,比如对于只有三层的结构将得到这样的数据01 0101 null null01 0102 null null01 0102 010201 null01 0102 010202 null01 0103 010301 0103010101 0103 01030101 null01 010201 null null01 010202 null null01 010301 null null0102 010201 null null0102 010202 null null0103 010301 01030101 null0103 01030101 null null移除重复项(当一行石林一行的子集)得到01 010101 010201 0102 01020101 0102 01020201 0103 010301 01030101一种变相的树图表述,再加工输出结果。 呵呵,小修正下:using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Text;namespace ConsoleApplication1{ class Program { class Data { public int Level { get; set; } public string ID { get; set; } public string Name { get; set; } public List<Data> Childs { get; set; } public Data() { Childs = new List<Data>(); } public Data this[string id] { get { if (ID == id) return this; Data data = null; if (Childs.Count > 0) { data = Childs.Where(x => x.ID == id).SingleOrDefault(); if (data == null) { foreach (var item in Childs) { data = item[id]; if (data != null) break; } } } return data; } } public override string ToString() { string TableChar = ""; for (int i = 0; i < Level; i++) TableChar += "\t"; StringBuilder sb = new StringBuilder(); if (Childs.Count == 0) { sb.Append(string.Format("{0}{{name:{1}}}", TableChar, Name)); } else { sb.AppendLine(string.Format("{0}{{name:{1}[", TableChar, Name)); for (int i = 0; i < Childs.Count; i++) { sb.AppendLine("\t" + Childs[i].ToString() + ((i == Childs.Count - 1) ? "" : ",")); } sb.Append(string.Format("\t{0}]}}", TableChar)); } return sb.ToString(); } } static void Main(string[] args) { DataTable dt = new DataTable(); dt.Columns.Add("id"); dt.Columns.Add("name"); dt.Rows.Add("01", "电脑"); dt.Rows.Add("02", "手机"); dt.Rows.Add("03", "电视机"); dt.Rows.Add("0101", "联想"); dt.Rows.Add("0102", "华硕"); dt.Rows.Add("0103", "惠普"); dt.Rows.Add("0201", "诺基亚"); dt.Rows.Add("0202", "三星"); dt.Rows.Add("0301", "夏普"); dt.Rows.Add("0302", "海尔"); dt.Rows.Add("020101", "N81"); dt.Rows.Add("020102", "N95"); var list = (from x in dt.Rows.Cast<DataRow>() group x by x["id"].ToString().Length into g select new { Key = g.Key, Items = g.ToList() }).OrderBy(x => x.Key); List<Data> DataRoot = new List<Data>(); list.ToList() .ForEach(x => { if (x.Key == 2) { x.Items.ForEach(y => DataRoot.Add(new Data() { ID = y["id"].ToString(), Name = y["Name"].ToString(), Level = x.Key / 2 - 1 })); } else { x.Items.ForEach(y => { Data data = null; foreach (var item in DataRoot) { data = item[y["id"].ToString().Substring(0, y["id"].ToString().Length - 2)]; if (data != null) break; } if (data != null) { data.Childs.Add(new Data() { ID = y["id"].ToString(), Name = y["Name"].ToString(), Level = x.Key / 2 - 1 }); } }); } }); StringBuilder sb = new StringBuilder(); for (int i = 0; i < DataRoot.Count; i++) { sb.Append(DataRoot[i].ToString() + ((i == DataRoot.Count - 1) ? "" : ",\r\n")); } string Result = sb.ToString(); Console.WriteLine(Result); } }} 求一个不会被主线程卡死的进度条或循环条 ReadFile读取不了缓冲区的数据? 求加解密的源码,提供者单独给100分 这个循环错在哪里呢? 拼音过滤 Winform中TextBox控件能否实现输入拼音下拉框选择数据内容 我下载的petshop在iis下无法运行 怎样去除html格式 用消息如何激活右下角目标的窗口? 新手求助 问个傻问题,如何单步调试含有Console.ReadLine()的CS 求一个正则表达式 如何写以下这个嵌套循环
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
class Data
{
public int Level { get; set; }
public string ID { get; set; }
public string Name { get; set; }
public List<Data> Childs { get; set; }
public Data() { Childs = new List<Data>(); } public Data this[string id]
{
get
{
if (ID == id) return this;
Data data = null;
if (Childs.Count > 0)
{
data = Childs.Where(x => x.ID == id).SingleOrDefault();
if (data == null)
{
foreach (var item in Childs)
{
data = item[id];
if (data != null) break;
}
}
}
return data;
}
}
public override string ToString()
{
string TableChar = "";
for (int i = 0; i < Level; i++) TableChar += "\t";
StringBuilder sb = new StringBuilder();
if (Childs.Count == 0)
{
sb.Append(string.Format("{0}{{name:{1}}}", TableChar, Name));
}
else
{
sb.AppendLine(string.Format("{0}{{name:{1}[", TableChar, Name));
for (int i = 0; i < Childs.Count; i++)
{
sb.AppendLine("\t" +
Childs[i].ToString() + ((i == Childs.Count - 1) ? "" : ","));
}
sb.Append(string.Format("\t{0}]}}", TableChar));
}
return sb.ToString();
}
} static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("name");
dt.Rows.Add("01", "电脑");
dt.Rows.Add("02", "手机");
dt.Rows.Add("03", "电视机");
dt.Rows.Add("0101", "联想");
dt.Rows.Add("0102", "华硕");
dt.Rows.Add("0103", "惠普");
dt.Rows.Add("0201", "诺基亚");
dt.Rows.Add("0202", "三星");
dt.Rows.Add("0301", "夏普");
dt.Rows.Add("0302", "海尔");
dt.Rows.Add("020101", "N81");
dt.Rows.Add("020102", "N95");
var list = (from x
in dt.Rows.Cast<DataRow>()
group x by x["id"].ToString().Length into g
select new { Key = g.Key, Items = g.ToList() }).OrderBy(x => x.Key);
List<Data> DataRoot = new List<Data>();
list.ToList()
.ForEach(x =>
{
if (x.Key == 2)
{
x.Items.ForEach(y => DataRoot.Add(new Data()
{
ID = y["id"].ToString(),
Name = y["Name"].ToString(),
Level = x.Key / 2 - 1
}));
}
else
{
x.Items.ForEach(y =>
{
Data data = null;
foreach (var item in DataRoot)
{
data = item[y["id"].ToString().Substring(0, y["id"].ToString().Length - 2)];
if (data != null) break;
}
if (data != null)
{
data.Childs.Add(new Data()
{
ID = y["id"].ToString(),
Name = y["Name"].ToString(),
Level = x.Key / 2 - 1
});
}
});
}
});
StringBuilder sb = new StringBuilder();
for (int i = 0; i < DataRoot.Count; i++)
{
sb.AppendLine(DataRoot[i].ToString());
}
string Result = sb.ToString();
Console.WriteLine(Result);
}
}
}
{name:电脑[
{name:联想},
{name:华硕},
{name:惠普}
]}
{name:手机[
{name:诺基亚[
{name:N81},
{name:N95}
]},
{name:三星}
]}
{name:电视机[
{name:夏普},
{name:海尔}
]}Press any key to continue . . .
自己Join自己如dt join dt按照条件 d2=>d2.Id.StartWith(d1.Id) && d2.Id != d1.Id
将结果存到一个新的dt中,列为 Id1 Id2 (Id2开头部分为Id1),再用新的dt2 jion dt得到dt3(Id,Id2,Id3)
继续join直到Idn的值全部为null,比如对于只有三层的结构将得到这样的数据
01 0101 null null
01 0102 null null
01 0102 010201 null
01 0102 010202 null
01 0103 010301 01030101
01 0103 01030101 null
01 010201 null null
01 010202 null null
01 010301 null null
0102 010201 null null
0102 010202 null null
0103 010301 01030101 null
0103 01030101 null null移除重复项(当一行石林一行的子集)得到
01 0101
01 0102
01 0102 010201
01 0102 010202
01 0103 010301 01030101一种变相的树图表述,再加工输出结果。
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;namespace ConsoleApplication1
{
class Program
{
class Data
{
public int Level { get; set; }
public string ID { get; set; }
public string Name { get; set; }
public List<Data> Childs { get; set; }
public Data() { Childs = new List<Data>(); } public Data this[string id]
{
get
{
if (ID == id) return this;
Data data = null;
if (Childs.Count > 0)
{
data = Childs.Where(x => x.ID == id).SingleOrDefault();
if (data == null)
{
foreach (var item in Childs)
{
data = item[id];
if (data != null) break;
}
}
}
return data;
}
}
public override string ToString()
{
string TableChar = "";
for (int i = 0; i < Level; i++) TableChar += "\t";
StringBuilder sb = new StringBuilder();
if (Childs.Count == 0)
{
sb.Append(string.Format("{0}{{name:{1}}}", TableChar, Name));
}
else
{
sb.AppendLine(string.Format("{0}{{name:{1}[", TableChar, Name));
for (int i = 0; i < Childs.Count; i++)
{
sb.AppendLine("\t" +
Childs[i].ToString() + ((i == Childs.Count - 1) ? "" : ","));
}
sb.Append(string.Format("\t{0}]}}", TableChar));
}
return sb.ToString();
}
} static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("name");
dt.Rows.Add("01", "电脑");
dt.Rows.Add("02", "手机");
dt.Rows.Add("03", "电视机");
dt.Rows.Add("0101", "联想");
dt.Rows.Add("0102", "华硕");
dt.Rows.Add("0103", "惠普");
dt.Rows.Add("0201", "诺基亚");
dt.Rows.Add("0202", "三星");
dt.Rows.Add("0301", "夏普");
dt.Rows.Add("0302", "海尔");
dt.Rows.Add("020101", "N81");
dt.Rows.Add("020102", "N95");
var list = (from x
in dt.Rows.Cast<DataRow>()
group x by x["id"].ToString().Length into g
select new { Key = g.Key, Items = g.ToList() }).OrderBy(x => x.Key);
List<Data> DataRoot = new List<Data>();
list.ToList()
.ForEach(x =>
{
if (x.Key == 2)
{
x.Items.ForEach(y => DataRoot.Add(new Data()
{
ID = y["id"].ToString(),
Name = y["Name"].ToString(),
Level = x.Key / 2 - 1
}));
}
else
{
x.Items.ForEach(y =>
{
Data data = null;
foreach (var item in DataRoot)
{
data = item[y["id"].ToString().Substring(0, y["id"].ToString().Length - 2)];
if (data != null) break;
}
if (data != null)
{
data.Childs.Add(new Data()
{
ID = y["id"].ToString(),
Name = y["Name"].ToString(),
Level = x.Key / 2 - 1
});
}
});
}
});
StringBuilder sb = new StringBuilder();
for (int i = 0; i < DataRoot.Count; i++)
{
sb.Append(DataRoot[i].ToString() + ((i == DataRoot.Count - 1) ? "" : ",\r\n"));
}
string Result = sb.ToString();
Console.WriteLine(Result);
}
}
}