我有一个Items
表和ItemVotes
表,每个用户可以放弃投票或向下投票到一个项目和他/她的投票决定由ItemVotes
表中的bool
财产。现在我想要有一个Items
排行榜的清单,为此,我们需要得到true
票数减去false
票数。LINQ查询计算投票计数
_ItemsService.GetAll(x => x.ItemVotes //here I have to order by vote)
UPDATE:
项目实体:
public class Items : Entity<int>
{
public int UserId { get; set; }
public DateTime Date { get; set; }
public string Subject { get; set; }
public virtual ICollection<ItemVotes> ItemVotes { get; set; }
}
ItemVotes实体:
public class ItemVotes : Entity<int>
{
public int ItemId { get; set; }
public int UserId { get; set; }
public DateTime Date { get; set; }
public bool TypeVote { get; set; }
public virtual Items Items { get; set; }
public virtual Users Users { get; set; }
}
更新2:
我用吉拉德格林的答案在动作是这样的:
public ActionResult TopItems()
{
var items = _ItemsService.GetAllQuerable(x => x)
.OrderBy(item => item.ItemsVote.Sum(vote => vote.TypeVote ? 1 : -1));
return View(items);
}
这里是GetAllQuerable()
功能:
public IEnumerable<T> GetAllQuerable(
Expression<Func<T,
bool>> filter = null,
Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
string includeProperties = "")
{
return _repository.GetAllQuerable(filter, orderBy, includeProperties);
}
这将有助于展示更广泛的代码范围和你的对象 –
@GiladGreen我已经更新了它。 – mohammad
看到我的回答在 –