locked
LINQ query for nested model returning null RRS feed

  • Question

  • User-1353617951 posted

    I have a nested model to apply select query using LINQ. I have a list of users ,I want to fetch all the posts of all the users , all the comments on those posts and all the likes on the posts and after that on the resulting list ,I am using OrderByDescending first on the basis of Date of the post and ThenByDescending on Time of the post.I have written a LINQ query but It's returning NULL even I have the corresponding data on the tables. Here are my models:<br />

     public class UserInfo : LogInDetail
        {
            public Int64 UserId { get; set; }
            [Required]
            [Display(Name = "Re-Enter Password")]
            [DataType(DataType.Password)]
            [Compare("UserPassword", ErrorMessage = "Passwords must match")]
            public string ConfirmPassword { get; set; }
            [Required]
            public string Gender { get; set; }
            [Required]
            public string Name { get; set; }
            [Required]
            [DataType(DataType.PhoneNumber)]
            [Display(Name = "Mobile Number")]
            [RegularExpression(@"^[789]\d{9}$", ErrorMessage = "Must Provide a valid contact number")]
            public Int64 ContactNo { get; set; }
            [Required]
            [DataType(DataType.Date)]
            [Display(Name = "Date of birth")]
            public DateTime DateOfBirth { get; set; }
            [Required]
            public string Country { get; set; }
    
            public virtual ICollection<FriendsRelation> friends { get; set; }
            public virtual ICollection<UserPosts> posts { get; set; }
            public virtual ICollection<PostComment> comments { get; set; }
            public virtual ICollection<PostLikes> likes { get; set; }
        }
    
    
    public class UserPosts
        {
            public Int64 PostId { get; set; }
            public Int64 UserId { get; set; }
    
            [DataType(DataType.MultilineText)]
            public string PostText { get; set; }
            public string PostPicture { get; set; }
            [DataType(DataType.Time)]
            public string Time { get; set; }
            [DataType(DataType.Date)]
            [DisplayFormat(DataFormatString = "{0:MM-dd-yyyy}")]
            public DateTime Date { get; set; }
            public int LikeCount { get; set; }
            public virtual ICollection<PostComment> comments { get; set; }
            public virtual ICollection<PostLikes> likes { get; set; }
            [Required]
            [ForeignKey("UserId")]
            public virtual UserInfo user { get; set; }
        }
    
    public class PostComment
        {
            public Int64 PostId { get; set; }
            public Int64 UserId { get; set; }
            [Required]
            [DataType(DataType.MultilineText)]
            public string CommentText { get; set; }
            [DataType(DataType.Time)]
            public string Time { get; set; }
            [DataType(DataType.Date)]
            public System.DateTime Date { get; set; }
    
            [ForeignKey("UserId")]
            public virtual UserInfo userInfo { get; set; }
            [ForeignKey("PostId")]
            public virtual UserPosts userPosts { get; set; }
        }
    public class PostLikes
        {
            public Int64 PostId { get; set; }
            public Int64 UserId { get; set; }
            [Required]
            [ForeignKey("PostId")]
            public virtual UserPosts post { get; set; }
            [Required]
            [ForeignKey("UserId")]
            public virtual UserInfo user { get; set; }
        }
    <br />
    Here is the LINQ query that I am using:
    var allPosts = from user in userIds
                               select new UserDetails
                               {
                                   UserId = user.UserId,
                                   posts = (from post in context.UserPosts
                                            where post.UserId == user.UserId
                                            select new UserPosts.Models.UserPosts
                                            {
                                                PostId = post.PostId,
                                                UserId = user.UserId,
                                                comments = (from comment in context.PostComments
                                                            where comment.PostId == post.PostId
                                                            select new UserPosts.Models.PostComment
                                                            {
                                                                PostId = post.PostId,
                                                            }).ToList(),
                                                likes = (from like in context.PostLikes
                                                         where like.PostId == post.PostId
                                                         select new UserPosts.Models.PostLikes
                                                         {
                                                             PostId = post.PostId
                                                         }).ToList()
                                            }).OrderByDescending(m=>m.Date).ThenByDescending(m=>m.Time).ToList(),
                               };

    Thursday, April 9, 2015 11:48 AM

Answers

  • User1711366110 posted

    have a nested model to apply select query using LINQ. I have a list of users ,I want to fetch all the posts of all the users , all the comments on those posts and all the likes on the posts and after that on the resulting list

      As per your case, the following link which is having similar situation of Sort a list and all its nested objects using Linq that may guide you to fix the issue :

    http://stackoverflow.com/questions/18853573/sort-a-list-and-all-its-nested-objects-using-linq

    --
    with regards,
    Edwin

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 10, 2015 5:11 AM