我有一張表可以在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
任何幫助將不勝感激..
請提供您已經嘗試的代碼。 –
@ Cubicle.Jockey我提出了我的問題 – nader