2016-04-30 135 views
1

我有一張表可以在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服務器此功能讓所有的孩子:

[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服務器:

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

@ Cubicle.Jockey我提出了我的問題 – nader

回答

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); 
+0

謝謝但是,這是一個靜態的方式來實現這一點。我想動態生成兒童。我可以動態生成Linq表達式嗎? – nader

+0

動態獲取兒童,你必須使用嵌套循環(可能是遞歸函數),並得到所有的兒童,如獲得第一級檢查,如果它有第二級和第二級檢查它是否有第三級..等 – Sachin

+0

謝謝我試試看看會發生什麼 – nader