2012-10-05 93 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的新表...並非特定的一個。 –