使用外键GroupBy多个表

本文关键字:GroupBy | 更新日期: 2023-09-27 18:05:45

我有两个表,我使用ViewDate列计算Views表的Post Views,然后我想使用.GroupBy获得PostTItle使用外键PostID的实体框架。

Posts:

PostID   PostTitle
--------------------
1         post one
2         post two 
3         post three
4         post four
5         post five
6         post six

Views table:

ViewID     ViewDate             PostID   
---------------------------------------
  1        2015 07 17 19:00:00        1
  2        2015 07 17 20:00:00        1
  3        2015 07 17 21:00:00        2
  4        2015 07 18 19:00:00        2
  5        2015 07 19 19:00:00        2
  6        2015 07 21 19:00:00        1
  7        2015 07 23 19:00:00        2

到目前为止,这是我所做的

    return _db.ObjectSet.Where(p => DateTime.Now >= EntityFunctions.AddDays(p.ViewDate, -14))
        .GroupBy(y => y.PostID, y => y.ViewDate, (ID, Date) => new ExampleViewModel
        {
            Post_ID = ID,
            View_Date = Date.Count()
        }).OrderByDescending(z => z.View_Date).Take(5);

但是使用这个解决方案,我只能将Post_IDView_Date分配给ExampleViewModel,我如何使用外键获得PostTitle ?

注意:我正在努力获得最近14天内观看次数最多的(热门)帖子

请帮预期输出:

Title:post one, Id:1, Views:3
Title:post two, Id:2, Views:4

使用外键GroupBy多个表

一种解决方案是在这些实体之间应用join,并在您想要分组的字段中包含PostTitle:

var query= (from v in db.Views
            join p in db.Posts on v.PostID equals p.Id
            where  DbFunctions.DiffDays(v.ViewDate,DateTime.Now)<=14
            group new{v,p} by new {v.PostID, p.PostTitle, v.ViewDate} into g
            let count=g.Count()
            orderby count descending
            select new{ Post_ID=g.Key.PostID, View_Date=count, Title= g.Key.PostTitle}
           ).Take(5);

正如你所看到的,我使用DbFunction类而不是EntityFunctionDbFunctions类是在实体框架6中引入的,它是单独从。net框架中发布的。对于从6.0开始使用EF版本的任何新应用程序,您应该使用DbFunctions类。无论如何,如果你现在不想使用这个类,你也可以使用EntityFunctions.DiffDays方法。

如果两个实体是相关的:

public class Post
{
  public int ID{get;set;}
  // ...
  public virtual ICollection<View> Views{get;set;}
}
public class View
{
  public int ID{get;set;}
  public int PostID{get;set;}
  // ...
  public virtual Post Post{get;set;}
}

你也可以这样做:

var query= (from v in db.Views
            where  DbFunctions.DiffDays(v.ViewDate,DateTime.Now)<=14
            group v by new {v.PostID, v.ViewDate} into g
            let count=g.Count()
            orderby count descending
            select new{ Post_ID=g.Key.PostID, View_Date=count, Title= g.First().Post.PostTitle}
           ).Take(5);

更新1

为了避免使用EntityFunctions类,您可以在当前日期减去14天,并在查询中直接比较两个日期:

 var date = DateTime.Now.AddDays(-14);
 var query= (from v in db.Views
             join p in db.Posts on v.PostID equals p.Id
             where  v.ViewDate>=date
             group new{v,p} by new {v.PostID, p.PostTitle, v.ViewDate} into g
             let count=g.Count()
             orderby count descending
             select new{ Post_ID=g.Key.PostID, View_Date=count, Title= g.Key.PostTitle}
         ).Take(5);

更新2

那是因为你按日期分组。要获得预期的结果,您需要从正在分组的元素中删除该字段:

  var query= (from v in db.Views
              join p in db.Posts on v.PostID equals p.Id
              where  v.ViewDate>=date
              group new{v,p} by new {v.PostID, p.PostTitle} into g
              let count=g.Count()
              orderby count descending
              select new{ Post_ID=g.Key.PostID, View_Date=count, Title= g.Key.PostTitle}
             ).Take(5);

如果你有Posts表在你的上下文中,你可以从那里检索PostId:

return _db.ObjectSet.Where(p => DateTime.Now >= EntityFunctions.AddDays(p.ViewDate, -14))
        .GroupBy(y => y.PostID, y => y.ViewDate, (ID, Date) => new ExampleViewModel
        {
            Post_ID = ID,
            View_Date = Date.Count(),
            Title = _db.Posts.Find(ID).PostTitle
        }).OrderByDescending(z => z.View_Date).Take(5);