How Do I Count Navigation Properties with Specific Values?
I have a couple queries for counting everything with specific integer value. The first one works, but the second fails. The one that works is:
int paarisgold = Posts.Where((Posts p) => p.Cap == 3).Count();
if(paarisgold > 0)
{
Capnoindex = true;
}
The one that fails is:
int paarisgoldcomment = Comments.Count((Comments c) => c.PostNavigation.Cap == 3);
if (paarisgoldcomment > 0)
{
Capnoindex = true;
}
How do I change the second one so that I can count the number of c.PostNavigation.Cap that are equal to 3?
ASP.NET Core
-
Brando Zhang-MSFT 2,956 Reputation points • Microsoft Vendor
2021-09-24T01:53:02.463+00:00 Hi @PostAlmostAnything ,
Could you please share the Comments model's makeup? Besides, the Count method normally is used to calculate comments' value. For example this Comments is List<Comment>, then the count function could set some Comment's condition. I suggest you could try this:
int paarisgoldcomment = Comments.Count(c => c.PostNavigation.Cap == 3);
-
PostAlmostAnything 1 Reputation point
2021-09-25T05:55:26.867+00:00 The relevant parts of the comments model look like this:
public partial class Comments { public int Commentid { get; set; } public string Comment { get; set; } public int? Post { get; set; } public DateTime? Date { get; set; } public bool Active { get; set; } public string Userid { get; set; } public virtual Posts PostNavigation { get; set; } public virtual ICollection<Images> Images { get; set; } public virtual ICollection<Likes> Likes { get; set; } public virtual ICollection<Ratings> Ratings { get; set; } public Comments() { Images = new HashSet<Images>(); Likes = new HashSet<Likes>(); Ratings = new HashSet<Ratings>(); } }
Then the Posts model has the following relevant part:
public int? Cap { get; set; } public virtual ICollection<Comments> Comments { get; set; } public Posts() { Comments = new HashSet<Comments>(); Images = new HashSet<Images>(); Likes = new HashSet<Likes>(); Ratings = new HashSet<Ratings>(); }
-
PostAlmostAnything 1 Reputation point
2021-09-25T05:59:10.71+00:00 The Comments object in the razor page is created by calling:
Comments = await PostService.GetCommentsbyUserName(ApplicationUser.UserName);
The service class contains this:
public async Task<List<Comments>> GetCommentsbyUserName(string username) { string userid = (await _userManager.FindByNameAsync(username.ToString().ToLower())).Id.ToString(); return (from p in await GetComments() where p.Userid == userid.ToString() select p into d orderby d.Commentid descending select d).ToList(); }
-
PostAlmostAnything 1 Reputation point
2021-09-25T06:02:20.097+00:00 The relevant parts of GetComments are as follows:
public async Task<Comments[]> GetComments() { return await (from c in _context.Comments select c into cs select new Comments { Commentid = cs.Commentid, Comment = cs.Comment, Post = cs.Post, Date = cs.Date, Active = cs.Active, Userid = cs.Userid, PostNavigation = new Posts { Title = cs.PostNavigation.Title, Active = cs.PostNavigation.Active, Cap = cs.PostNavigation.Cap }, Ratings = cs.Ratings.Select((Ratings r) => new Ratings { Commentnumber = r.Commentnumber, Rating = r.Rating }).ToArray(), Images = cs.Images.Select((Images i) => new Images { Image = i.Image, Imagename = i.Imagename }).ToArray(), Likes = cs.Likes.Select((Likes l) => new Likes { Likeid = l.Likeid, Commentid = l.Commentid }).ToArray() }).ToArrayAsync(); }
-
PostAlmostAnything 1 Reputation point
2021-09-25T06:04:09.367+00:00 I am trying to count the number of posts with a Cap value of 3 from a part of the site that displays comments only.
-
Brando Zhang-MSFT 2,956 Reputation points • Microsoft Vendor
2021-09-28T07:32:14.557+00:00 Could you please tell me what's the result you have always return 0 or you faced some error message?
-
PostAlmostAnything 1 Reputation point
2021-09-29T05:54:18.68+00:00 No error message. Just 0
-
Brando Zhang-MSFT 2,956 Reputation points • Microsoft Vendor
2021-10-08T05:42:46.83+00:00 Could you please tell me which ORM you have used in your project? EF core or Dapper or else? It seems this is related with lazyloading or something else.
-
PostAlmostAnything 1 Reputation point
2021-10-11T01:11:26.937+00:00 EF Core
Sign in to comment