解决方案 »
- 关于App.config的问题
- C#操作Excel需要添加哪个引用?奇怪了
- VS2005怎么做打包程序能使程序在安装结束后,自动执行程序。(像QQ一样安装结束后,它会自动启动qq程序)[顶者有分]
- asp.net在2000系统下连接db2数据库问题
- 用SOCKET接收大数据时,各位是如何处理的???
- 我想自定义个表格,表格能自动显示数据库数数据。通过表格能对数据库进行增删改
- 关于用户控件的一个问题,请大家帮忙看一下!谢谢!
- C#中,如何把DropDownList控件与数据库中的一个表建立连接,以实现下拉菜单的的效果
- 谁有控件,能把网页上的一个文体框的文字导出到word,[email protected]
- VS下调试运行时正常,直接运行exe文件时会有卡顿
- C#数据库无法连接
- C#下怎么从指针指向的byte数组转换成bitmap类型
using System.Collections.Generic;
using System.Linq;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<Question> QuestionList = new List<Question>(); Question Q11 = new Question()
{
Title = "1.1",
Score = 5,
}; Question Q12 = new Question()
{
Title = "1.2",
Score = 5,
}; Question Q13 = new Question()
{
Title = "1.3",
Score = 5,
}; List<Question> Q13List = new List<Question>(); Question Q131 = new Question()
{
Title = "1.3.1",
Score = 5,
}; Question Q132 = new Question()
{
Title = "1.3.2",
Score = 5,
}; List<Question> Q132List = new List<Question>(); Question Q1321 = new Question()
{
Title = "1.3.2.1",
Score = 5,
}; Question Q1322 = new Question()
{
Title = "1.3.2.2",
Score = 5,
}; Question Q1323 = new Question()
{
Title = "1.3.2.3",
Score = 5,
}; Q132.Questions = Q132List; Q13List.Add(Q131);
Q13List.Add(Q132); Q13.Questions = Q13List; List<Question> Q1List = new List<Question>();
Q1List.Add(Q11);
Q1List.Add(Q12);
Q1List.Add(Q13); Question Q1 = new Question()
{
Title = "First One",
Score = 25,
Questions = Q1List,
}; Question Q2 = new Question()
{
Title = "Second",
Score = 25,
}; Question Q3 = new Question()
{
Title = "Third",
Score = 25,
}; QuestionList.Add(Q1);
QuestionList.Add(Q2);
QuestionList.Add(Q3); var all = GetAll(QuestionList);
Console.WriteLine("现在总共有 {0} 个Question。", all.Count()); var search = (from t in all
let q = t.Item1
where q.Title == "1.3.2" && q.Score == 5
select t).ToList();
Console.WriteLine("找到{0}个符合条件的Question。", search.Count); if (search.Any(r => r.Item1.Questions != null && r.Item1.Questions.Count > 0))
throw new Exception("Question的Questions集合不是空的,不能删除。"); foreach (var r in search)
r.Item2.Questions.Remove(r.Item1); //从父Question的Questions属性中删除子Quenstion Console.WriteLine("总共有 {0} 个Question。", GetAll(QuestionList).Count());
Console.WriteLine("_________________按任意键结束。");
Console.ReadKey();
} /// <summary>
/// 返回所有的Question极其父Question。
/// </summary>
/// <param name="list">要遍历的树。</param>
/// <returns>返回子Question及其父Question的 Tuple 结构列表。
/// 对于顶级Question,则父Question为null。</returns>
public static IEnumerable<Tuple<Question, Question>> GetAll(List<Question> list)
{
foreach (var r in GetAll(list, null))
yield return r;
} private static IEnumerable<Tuple<Question, Question>> GetAll(List<Question> list, Question parent)
{
foreach (var q in list)
{
yield return new Tuple<Question, Question>(q, parent);
var children = q.Questions;
if (children != null && children.Count > 0)
foreach (var r in GetAll(q.Questions, q))
yield return r;
}
} } public class Question
{
public List<Question> Questions { get; set; } public string Title { get; set; } public int Score { get; set; }
}
}
要删除它,必须先找到它。而要找到它,就需要递归搜索。在数据结构上,由于需要知道它的parent才能删除它,因此搜索的结果是得到 Tuple<Question,Question> 这样的“子-父”结构。在搜索功能上,使用了c#的迭代器功能,方便于将搜索应用到各种灵活的枚举功能的场合。例如使用 Linq 中。