动态选择儿童记录

时间:2016-04-30 05:12:44

标签: c# sql-server linq

我有一个评论表,我可以在n个级别添加记录,这意味着我可以评论它是在评论的第n级回复中。

我的问题是如何使用linq动态选择n级注释?

例如,我希望评论回复第5级或第2级或第n级。

这是我的表

public partial class {

public Comment()
{
    this.Comments1 = new HashSet<Comment>();
}

public int CommentId { get; set; }
public Nullable<int> ParentId { get; set; }
public string Title { get; set; }

public virtual ICollection<Comment> Comments1 { get; set; }
public virtual Comment Comment1 { get; set; }
}

直到现在我使用linq并在sql-server中调用此函数来获取所有子项:

[DbFunction("Ents", "cmTree")]
   public virtual IQueryable<ContentComment> cmTree(string topLevelComments)
   {
       var topLevelCommentsParameter = topLevelComments != null ?
           new ObjectParameter("topLevelComments", topLevelComments) :
           new ObjectParameter("topLevelComments", typeof(string));

       return ((IObjectContextAdapter)this).ObjectContext.CreateQuery<ContentComment>("[Entities].[cmTree](@topLevelComments)", topLevelCommentsParameter);
   }

然后在sql-server中:

ALTER FUNCTION [dbo].[cmTree] 
(
-- Table types seems goods here.
-- but in application-level, linq to sql technology dose not support table types.
-- TupleValue type can user for future use.
   @topLevelComments NVARCHAR(max)

)
RETURNS @resultTable TABLE (
[Id] [bigint] primary KEY NOT NULL,
[AuthorUserId] [int] NULL,
[AuthorName] [nvarchar](128) NULL,
[AuthorEmail] [nvarchar](256) NULL,
[AuthorUrl] [nvarchar](512) NULL,
[AuthorIp] [nvarchar](100) NULL,
[InsertDateTime] [datetime] NOT NULL,
[BodyContent] [nvarchar](max) NOT NULL,
[IsApproved] [bit] NOT NULL,
[IsAlertable] [bit] NOT NULL,
[ContentId] [bigint] NOT NULL,
[ParentCommentId] [bigint] NULL,
[VerifierUserID] [int] NULL,
[VerifyDateTime] [datetime] NULL,
[Status] [bit] NOT NULL,
[LastModifierUserId] [int] NULL,
[LastModifiedDateTime] [datetime] NULL
)

AS
BEGIN
with CommentTableExpression As (

-- Anchor entities
select rC.Id, rc.AuthorUserId, rc.AuthorName, rc.[AuthorEmail], rc.[AuthorUrl], rc.[AuthorIp], rc.[InsertDateTime], rc.[BodyContent], rc.[IsApproved], rc.[IsAlertable], rc.[ContentId], rc.[ParentCommentId], rc.[VerifierUserID], rc.[VerifyDateTime], rc.[Status], rc.[LastModifierUserId], rc.[LastModifiedDateTime] 
from dbo.ContentComments as rC
WHERE rc.ParentCommentId IN (SELECT * FROM dbo.CSVToTable(@topLevelComments))

union all

-- Recursive query execution
select child.Id, child.AuthorUserId, child.AuthorName, child.[AuthorEmail], child.[AuthorUrl], child.[AuthorIp], child.[InsertDateTime], child.[BodyContent], child.[IsApproved], child.[IsAlertable], child.[ContentId], child.[ParentCommentId], child.[VerifierUserID], child.[VerifyDateTime], child.[Status], child.[LastModifierUserId], child.[LastModifiedDateTime]

from dbo.ContentComments as child
inner join CommentTableExpression as t_Comment
on child.ParentCommentId = t_Comment.Id
where child.ParentCommentId is not NULL) -- commente contet=nt budan barasi shavad.

INSERT @resultTable Select * from CommentTableExpression

RETURN 
END

任何帮助将不胜感激..

1 个答案:

答案 0 :(得分:1)

最好在名为“ChildLevel”的表中再管理一个字段,并在该字段中添加回复级别。

但是如果你想使用相同的结构并希望管理,那么linq将获得5级孩子

var rec = dt.Comments().Where(t => t.Comment1 != null 
              && t.Comment1.Comment1 != null 
              && t.Comment1.Comment1.Comment1 != null 
              && t.Comment1.Comment1.Comment1.Comment1 != null );