GroupBy使用外键的多个表

时间:2015-07-31 22:51:27

标签: c# linq entity-framework group-by

我有两个表,我使用Views列计算ViewDate表中的帖子后视图然后我想使用外键使用PostTItle使用.GroupBy进行实体框架PostID

Posts表:

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

Views表:

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

2 个答案:

答案 0 :(得分:2)

一种解决方案可能是在这些实体之间应用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类是在Entity Framework 6中引入的,它与.NET Framework分开发布。对于使用从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);

答案 1 :(得分:0)

如果您的上下文中有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);