每次用户查看“新闻”页面时,视图的时间都会保存在LastTimeNewsPageWasViewed
实体的User
属性中。
我想知道自上次用户浏览新闻页面以来发布了多少新消息。
我的问题是我不知道如何在实体框架中找到适当的声明。
以下代码不起作用。但是如何归档这样的东西呢?
var totalCountOfNewNewsNotYetViewedByCurrentUser = context.Subscriptions
.Where(s => s.TvShow.News.CreatedOnDate > currentUser.LastTimeNewsPageWasViewed)
.Count();
答案 0 :(得分:1)
我最终使用了这个:
if (currentUser == null)
{
return 0;
}
// Here we create all "custom objects with custom properties (NewNotificationsTotalCount)" for each tv show.
var anonymousObjects = context.Subscriptions
.Where(s2 => s2.UserId == currentUser.Id && ((s2.TvShow.IsPrivate == false) || (s2.TvShow.IsPrivate == true && s2.UserId == currentUser.Id)))
.Select(s => new
{
NewNotificationsTotalCount = s.TvShow.News.Where(n => n.CreatedOnDatetime > currentUser.LastTimeViewedSubscriptions).Count()
});
var newNotificationsTotalCount = 0;
// Now we must go through all the anonymous objects we created before and sum together all new news for all tv shows else we would just get the count of new news for only one tv show (if we used something like anonymousObjects.FirstOrDefault() for example).
foreach (var oneAnonymousObject in anonymousObjects)
{
newNotificationsTotalCount = newNotificationsTotalCount + oneAnonymousObject.NewNotificationsTotalCount;
}
return newNotificationsTotalCount;