Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 关系的深度_C#_Lambda_.net 4.5 - Fatal编程技术网

C# 关系的深度

C# 关系的深度,c#,lambda,.net-4.5,C#,Lambda,.net 4.5,我试图弄明白如何获得一个实体与自身相关的关系的深度。也就是说,有一个基本注释项,可以有一个或多个回复注释链接到它 现在,对于每一条回复的评论,我想知道它们距离顶部节点有多深 ie,我想知道下图中评论的深度: 这是目前为止的代码,变量ResponseTo是此注释响应的关系,如果不是响应,则值为null var comments = db.Comments .Where(c => c.Post.ID == id && c.Status == Helpers.StatusCod

我试图弄明白如何获得一个实体与自身相关的关系的深度。也就是说,有一个基本注释项,可以有一个或多个回复注释链接到它

现在,对于每一条回复的评论,我想知道它们距离顶部节点有多深

ie,我想知道下图中评论的深度:

这是目前为止的代码,变量
ResponseTo
是此注释响应的关系,如果不是响应,则值为
null

var comments = db.Comments
.Where(c => c.Post.ID == id && c.Status == Helpers.StatusCode.Visible)
.Select(x => new CommentTemp()
    {
        Id = x.ID,
        Text = x.Text,
        Avatar = "-1",
        Username = (x.User != null ? x.User.UserName : "Anonymous"),
        UserID = (x.User != null ? (int?) x.User.Id : null),
        ResponseTo = (x.ResponseTo == null ?  null : (int?) x.ResponseTo.ID),
        CommentDepth = ??? // <--- how do i select the depth of the response to relations?
        Created = x.Created
    })
.ToList();
var comments=db.comments
.Where(c=>c.Post.ID==ID&&c.Status==Helpers.StatusCode.Visible)
.Select(x=>newcommenttemp()
{
Id=x.Id,
Text=x.Text,
阿凡达=“-1”,
用户名=(x.User!=null?x.User.Username:“匿名”),
UserID=(x.User!=null?(int?)x.User.Id:null),
ResponseTo=(x.ResponseTo==null?null:(int?)x.ResponseTo.ID),

CommentDepth=???//嗯,听起来你确实想在数据库中保存这些信息,除非有很好的理由不这样做。不管怎样,Linq都不是真正的递归友好型

您可以通过创建字典并递归地跟踪它来获得深度

int GetDepth(Comment comment, 
                IDictionary<int, Comment> comments,  /*key=commentId, val=Comment*/
                IDictionary<int, int> depthMemoization, /* key=commentId, val=depth*/
                int currentDepth = 0)
{
    if(depthMemoization.ContainsKey(comment.Id))
        return depthMemoization[comment.Id];

    if(comment.ParentId==null)
        return currentDepth;

    var parentComment = comments[comment.ParentId.Value];

    int calculatedDepth = GetDepth(parentComment, comments, 
                            depthMemoization,
                            ++currentDepth);

    depthMemoization[comment.Id] = calculatedDepth ;

    return depth;
}
intgetdepth(注释,
IDictionary注释,/*key=commentId,val=Comment*/
IDictionary depthmemodization,/*key=commentId,val=depthm*/
int currentDepth=0)
{
if(depthmemonization.ContainsKey(comment.Id))
返回depthmemonization[comment.Id];
if(comment.ParentId==null)
回流深度;
var parentComment=comments[comment.ParentId.Value];
int calculatedDepth=GetDepth(parentComment,comments,
深度情绪化,
++电流深度);
深度表情化[comment.Id]=计算深度;
返回深度;
}