Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用外键按多个表分组_C#_Linq_Entity Framework_Group By - Fatal编程技术网

C# 使用外键按多个表分组

C# 使用外键按多个表分组,c#,linq,entity-framework,group-by,C#,Linq,Entity Framework,Group By,我有两个表,我使用ViewDate列从Views表中计算Post视图,然后我想使用获取PostTItle。GroupBy用于使用外键的实体框架PostID 帖子表格: PostID PostTitle -------------------- 1 post one 2 post two 3 post three 4 post four 5 post five 6 post six ViewID

我有两个表,我使用
ViewDate
列从
Views
表中计算Post视图,然后我想使用
获取
PostTItle
。GroupBy
用于使用外键的实体框架
PostID

帖子
表格:

PostID   PostTitle
--------------------
1         post one
2         post two 
3         post three
4         post four
5         post five
6         post six
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
视图
表格:

PostID   PostTitle
--------------------
1         post one
2         post two 
3         post three
4         post four
5         post five
6         post six
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\u ID
View\u Date
分配给
ExampleViewModel
,如何使用外键获取
PostTitle

注意:我试图在过去14天内获得最多浏览量(热门)的帖子

请帮忙

预期输出:

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

如果您的上下文中有
Posts
表,您可以通过
postd
从那里检索它:

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

一种解决方案是在这些实体之间应用
连接
,并在要分组的字段中包含
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);
您也可以这样做:

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);
更新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);

不,不幸的是,这是
视图
表的
上下文。所以我不能那样访问它。但是
外键
视图
表中,我必须使用它来访问
PostTitle
。请回答为什么您的所有表不在同一上下文中?@Nimrand:因为我使用类库作为数据访问层。并使用
存储库
。在这个
模型上
我刚刚调用了
视图存储库
来访问
视图
表,
外键
应该会自动授予对
Posts
表的访问权限。您现在使用的方法是哪个类?仓库?你能发布你的上下文定义吗?如果
Posts
在该范围内不可用,则应在上下文中或通过提供另一上下文或存储库使其可用。我可以建议对相关的工作流程使用一个上下文,只需添加所需的内容即可访问。@ErwinRooijakkers我同意Erwin的观点。所有相关的表都应该在相同的上下文中。我还可能建议检查存储库和工作单元的设计模式。程序员经常会在很多方面出错,听起来你可能正是出于这个原因把自己编码到了一个角落里。在模型中,
View
Post
实体之间是否存在一对多关系?@octavioccl:只有一种关系,
posted
就是这种关系。谢谢你的回答,实际上这个web应用程序是用EF4制作的,他们也为该网站制作了web服务和应用程序。我试着和他们谈谈,让他们转到EF6和MVC5,但他们说我们不想在EF4和MVC3上多呆一段时间。新的要求是在7天、14天或一个月内获得热门职位。因此,我不能真正使用EF6来解决这个问题,但现在将尝试您的解决方案。当我使用
EntityFunctions.DiffDays
时,我在这个解决方案中遇到了这个错误,因为它在EF4上,“
{”Method'System.Nullable 1[System.Int32]DiffDays(System.Nullable 1[System.DateTime],System.Nullable 1[System.DateTime])不支持转换为SQL。”}
“当我使用
EntityFunctions.AddDays
时也是如此。。请回复,这应该是因为您使用的是EF 4的旧版本,我在EF 6+中测试了这两个版本,它们都有效。我认为
AddDays
过去对您有效,因为在这种情况下您可以这样做:
p.ViewDate>=EntityFunctions.AddDays(DateTime.Now,-14)
,您使用的是什么版本的EF?谢谢您的回复,我使用EF4是因为这是客户的要求。@Rabia,检查uptade 1