I am using dbcontext Code first to get a query base on this condition for the Classes (tables) below:
Creator != null && ArticleAttached != null && !IsCancelled
Problem :
How to get data from the classes (tables) and show
Article Title, No. of Likes, No. Of Comments, Total Assigned, TotalResponded
A) I use dbcontext as follows:
var ListArticles = dbcontext.LearningActivites
.Where(la => la.Creator != null && la.ArticleAttached != null && !la.IsCancelled)
.Select(la =>
new
{
Id = la.Id,
Title = la.ArticleAttached.ArticleTitle,
CreatedWhen = la.ArticleAttached.CreatedAt,
TTLComment = la.ArticleAttached.Comments.Count,
TTLOfLikes = la.ArticleAttached.StudentsLiked.Count + la.ArticleAttached.TeachersLiked.Count
});
This will get :
Article Title, Total of Likes, total Comment
B) How to get : Total Assigned and Total Responded base on the classes below?
var TotalAssigned = dbcontext.LearningActivites
.Where(la => la.Creator != null && la.ArticleAttached != null && !la.IsCancelled)
.Sum(la => la.ClassesAssigned.SelectMany(a => a.LearningActivitiesAssigned.LongCount));
C) How to joint the result from A + B ?
to show
Article Title, No. of Likes, No. Of Comments, Total Assigned, TotalResponded
Classes:
public class LearningActivity
{
public virtual ArticleCreator Creator { get; set; }
public virtual ArticleCreator EditedBy { get; set; }
public virtual Teacher CreatedByTeacher { get; set; }
public virtual Article ArticleAttached { get; set; }
public virtual Article ArticleAttachedByOther { get; set; }
public virtual IList<Class> ClassesAssigned { get; set; }
public virtual IList<Group> GroupsAssigned { get; set; }
public bool IsCancelled { get; set; }
}
public class Article
{
public string ArticleTitle {get;set;}
public virtual IList<Teacher> TeachersLiked { get; set; }
public virtual IList<Student> StudentsLiked { get; set; }
public virtual IList<ArticleComment> Comments { get; set; }
}
public class Student
{
public virtual IList<ArticleCommentStudent> Comments { get; set; }
}
public class Class
{
public virtual School School { get; set; }
public virtual IList<Student> Students { get; set; }
public virtual IList<LearningActivity> LearningActivitiesAssigned { get; set; }
}
public class Group
{
public virtual School School { get; set; }
public virtual IList<Student> Students { get; set; }
public virtual ApplicationUser Creator { get; set; }
}
Thanks
Can you use this to sum of Assigned
TotalAssigned = la.ClassesAssigned.Sum(x => x.LearningActivitiesAssigned.LongCount)
and
.GroupBy(la => la.ArticleAttached.ArticleTitle)