2012-10-05 89 views
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 
) 

对于这些表我有日期和邮政实体。

我怎样才能得到一个表的DateValue从日期和帖子数与该日期。

我需要将日期时间创建值与DateValue日期相匹配。

谢谢

米格尔

回答

1

我假设你Post■找与时俱进日期,所以你必须给他们截断的日期部分(如在DateTimeDate属性):

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() 
      } 
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() }); 
} 
+0

也许我解释自己错误。我正在寻找具有所有Dates.DateValue和Posts.Cound的新表...并非特定的一个。 –