这是我的数据库架构:
create table Post
(
PostId int primary key identity,
ImageUrl nvarchar(1024) not null,
[Description] nvarchar(256) not null,
UserId int foreign key references [User](UserId),
DateOfPost datetime not null,
Score int
)
create table Vote
(
VoteId int primary key identity,
UserId int foreign key references [User](UserId),
PostId int foreign key references Post(PostId),
VoteDirection bit not null,
DateOfVote datetime not null
)
基本上,我想按[VoteDirection == true] - [VoteDirection == false]
订购帖子。因此,20人投票“真实”,5投票“假”,用于订购的临时分数将为15。
是否还有一种方法可以使用DateOfVote参数选择日期范围?
这是我尝试过的:
// www.foo.com/top/today
public ActionResult Today()
{
var posts = postRepository.FindAllPosts().Where(x => x.DateOfPost == DateTime.Today)
.OrderByDescending(x => x.Votes.Where(c => c.VoteDirection == true) - x.Votes.Where(c => c.VoteDirection == false));
return View(posts);
}
我收到了错误:
错误2运算符' - '不能应用于类型的操作数 'System.Collections.Generic.IEnumerable' 和 'System.Collections.Generic.IEnumerable'
这是可以理解的,它们都是集合而不是实际的整数。但它应该说明我正在尝试做什么。有什么建议吗?
答案 0 :(得分:4)
只需使用Count
:
.OrderByDescending(x =>
x.Votes.Count(c => c.VoteDirection) - x.Votes.Count(c => !c.VoteDirection));