0

我有两张桌子:

create table dbo.Dates (
  Id int not null
    constraint Dates_Id_PK primary key clustered (Id),
  [DateValue] date not null
}

create table dbo.Posts (
  Id int identity not null 
    constraint Posts_Id_PK primary key clustered (Id),
  Created datetime not null,
  Title nvarchar (200) not null
)

对于这些表,我有 Date 和 Post 实体。

如何获得一个表格,其中包含 Dates 中的 DateValue 列以及该日期的帖子数。

我需要将 datetime Created 值与日期 DateValue 匹配。

谢谢,

米格尔

4

2 回答 2

1

我假设您Post的 s 有带时间的日期,因此您必须将它们截断为日期部分(如Datea 的属性DateTime):

from d in context.Dates
select new { 
             Date = d.DateValue,
             NrOfPosts = (from p in context.Posts
                 where EntityFunctions.TruncateTime(t.Created) == d.DateValue
                 select p).Count()
           }
于 2012-10-05T20:51:21.947 回答
0

您可以使用匿名类型。尝试以下代码段。

DateTime dateToMatch = GetDateToMatch();

using(YourEntities context = new YourEntities())
{
    var result = context.Dates.Where(d => d.DateValue == dateToMatch)
                              .Select(d => new { Date = d.DateValue, PostCount = d.Posts.Count() });
}
于 2012-10-05T01:05:23.297 回答