如何使用LinQ To Entities联合两组检索的数据 [英] How to joint two set of retrieved data using LinQ To Entities

查看:276
本文介绍了如何使用LinQ To Entities联合两组检索的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用dbcontext代码首先获得以下类中的这个条件的查询:



Creator!= null& &安培; ArticleAttached!= null&& !IsCancelled



问题:



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



文章标题,赞成数量,评论数量,总计分配,总回应

  A)我使用dbcontext如下:

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

});

这将得到:

文章标题,合计总数,总评论

B)如何获得:总分配和总体回答基于以下类别?

 $ $ c $ var var = IsCancelled)
.Sum(la => la.ClassesAssigned.SelectMany(a => a.LearningActivitiesAssigned.LongCount));

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



显示
文章标题,赞赞数,评论数,总计,总回应

类:

  public class LearningActivity 
{
public virtual ArticleCreator Creator {get;组; }
public virtual ArticleCreator EditedBy {get;组; }
public virtual教师CreatedByTeacher {get;组;
public virtual Article ArticleAttached {get;组;
public virtual Article ArticleAttachedByOther {get;组; }

public virtual IList< Class> ClassesAssigned {get;组; }
public virtual IList< Group> GroupsAssigned {get;组; }

public bool IsCancelled {get;组;



public class文章
{
public string ArticleTitle {get; set;}
public virtual IList&&Teacher老师组; }
public virtual IList< Student>学生组; }
public virtual IList< ArticleComment>评论{get;组; }
}


public class Student
{
public virtual IList< ArticleCommentStudent>评论{get;组; }
}



public class Class
{
public virtual School School {get;组; }
public virtual IList< Student>学生{get;组; }
public virtual IList< LearningActivity> LearningActivitiesAssigned {get;组; }

}

public class Group
{
public virtual School School {get;组; }
public virtual IList< Student>学生{get;组; }
public virtual ApplicationUser Creator {get;组;

}

谢谢

解决方案

你可以使用这个来分配分配的$ /

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



.GroupBy(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.Class esAssigned.Sum(x => x.LearningActivitiesAssigned.LongCount)
});


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)

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

这篇关于如何使用LinQ To Entities联合两组检索的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆