Linq-to-Sql: recursively get children(Linq-to-Sql:递归获取子项)
问题描述
I have a Comment table which has a CommentID and a ParentCommentID. I am trying to get a list of all children of the Comment. This is what I have so far, I haven't tested it yet.
private List<int> searchedCommentIDs = new List<int>();
// searchedCommentIDs is a list of already yielded comments stored
// so that malformed data does not result in an infinite loop.
public IEnumerable<Comment> GetReplies(int commentID) {
var db = new DataClassesDataContext();
var replies = db.Comments
.Where(c => c.ParentCommentID == commentID
&& !searchedCommentIDs.Contains(commentID));
foreach (Comment reply in replies) {
searchedCommentIDs.Add(CommentID);
yield return reply;
// yield return GetReplies(reply.CommentID)); // type mis-match.
foreach (Comment replyReply in GetReplies(reply.CommentID)) {
yield return replyReply;
}
}
}
2 questions:
- Is there any obvious way to improve this? (Besides maybe creating a view in sql with a CTE.)
- How come I can't yield a
IEnumerable <Comment>
to an IEnumerable<Comment>
, onlyComment
itself? - Is there anyway to use SelectMany in this situation?
I'd probably use either a UDF/CTE, or (for very deep structures) a stored procedure that does the same manually.
Note that if you can change the schema, you can pre-index such recursive structures into an indexed/ranged tree that lets you do a single BETWEEN query - but the maintenance of the tree is expensive (i.e. query becomes cheap, but insert/update/delete become expensive, or you need a delayed scheduled task).
Re 2 - you can only yield
the type specified in the enumeration (the T
in IEnumerable<T>
/ IEnumerator<T>
).
You could yield
an IEnumerable<Comment>
if the method returned IEnumerable<IEnumerable<Comment>>
- does that make sense?
Improvements:
- perhaps a udf (to keep composability, rather than a stored procedure) that uses the CTE recursion approach
- use
using
, sinceDataContext
isIDisposable
...
so:
using(var db = new MyDataContext() ) { /* existing code */ }
- LoadWith is worth a try, but I'm not sure I'd be hopeful...
- the list of searched ids is risky as a field - I guess you're OK as long as you don't call it twice... personally, I'd use an argument on a private backing method... (i.e. pass the list between recursive calls, but not on the public API)
这篇关于Linq-to-Sql:递归获取子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Linq-to-Sql:递归获取子项
基础教程推荐
- 从 VB6 迁移到 .NET/.NET Core 的最佳策略或工具 2022-01-01
- 使用 SED 在 XML 标签之间提取值 2022-01-01
- C# - 如何列出发布到 ASPX 页面的变量名称和值 2022-01-01
- C# - 将浮点数转换为整数...并根据余数更改整数 2022-01-01
- 我什么时候应该使用 GC.SuppressFinalize()? 2022-01-01
- 当键值未知时反序列化 JSON 2022-01-01
- 创建属性设置器委托 2022-01-01
- 覆盖 Json.Net 中的默认原始类型处理 2022-01-01
- Page.OnAppearing 中的 Xamarin.Forms Page.DisplayAlert 2022-01-01
- 如何使用OpenXML SDK将Excel转换为CSV? 2022-01-01