如何使用LinQ To Entities

时间:2017-06-07 08:18:42

标签: entity-framework-6 linq-to-entities

我首先使用dbcontext Code来获取基于此条件的查询,用于下面的类(表):

创作者!= null&& ArticleAttached!= null&& !IsCancelled

问题:

如何从类(表)中获取数据并显示

文章标题,喜欢的数量,评论数量,总分配,总回报率

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)如何获得:总分配和总回复基于以下类别?

 var TotalAssigned = dbcontext.LearningActivites
 .Where(la => la.Creator != null && la.ArticleAttached != null && !la.IsCancelled) 
 .Sum(la => la.ClassesAssigned.SelectMany(a => a.LearningActivitiesAssigned.LongCount));

C)如何联合A + B的结果?

显示
文章标题,喜欢的数量,评论数量,总分配,总回报率

类:

 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; }

}

由于

1 个答案:

答案 0 :(得分:0)

你能用它来分配

TotalAssigned = la.ClassesAssigned.Sum(x => x.LearningActivitiesAssigned.LongCount)

.GroupBy(la => la.ArticleAttached.ArticleTitle)

var ListArticles = dbcontext.LearningActivites
   .GroupBy(la => la.ArticleAttached.ArticleTitle)
   .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
        TotalAssigned = la.ClassesAssigned.Sum(x => x.LearningActivitiesAssigned.LongCount)
      });