Entity framework 在实体框架中匹配日期和计数
我有两张桌子:Entity framework 在实体框架中匹配日期和计数,entity-framework,Entity Framework,我有两张桌子: 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), Cre
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实体
如何获得一个表,该表包含DateValue from Dates列和具有该日期的帖子数量
我需要将datetime创建值与date DateValue匹配
谢谢,
米格尔您可以使用匿名类型。请尝试以下代码段
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() });
}
我假设您的
Post
s具有带时间的日期,因此您必须将它们截断为日期部分(如DateTime
的date
属性中所示):
也许我解释错了。我正在寻找一个新的表与所有日期。日期值和职位。咨询。。。不是特别的。
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()
}