c# - Linq to EF Include sub List for List -
given following method returns blogpost, parent blog object , list of comments blog post; trying determine if there way, within method, return list of comment replies each comment within list of comments.
here classes
public class comment { [key] public int commentid { get; set; } //[foreignkey("blogpost")] //[index("ix_blogpostindex", 1, isclustered = false, isunique = false)] public int blogpostid { get; set; } public blogpost blogpost { get; set; } [required] public string commenttext { get; set; } [required] public datetime commentposttime { get; set; } public list<reply> commentreplies { get; set; } [required] public string userfullname { get; set; } } public class reply { [key] public int replyid { get; set; } [required] public string replytext { get; set; } [required] public userprofile memberprofile { get; set; } [foreignkey("comment")] [index("ix_commentindex", 1, isclustered = false, isunique = false)] public int commentid { get; set; } public comment comment { get; set; } [required] public datetime replyposttime { get; set; } } and method
public blogpost getblogpostbyid(int blogpostid) { return _db.blogposts.where(e => e.blogpostid == blogpostid) .include(e => e.comments) .include(e => e.blog) .firstordefault(); } i can iterate through return object method , retrieve replies sure there must way, using linq, within method part of returned object rather run method so.
if "replies" navigation property on comments, can add include existing query:
.include("comments.replies") this works same way includes have, easier include nested navigation properties
Comments
Post a Comment