0

这是我的模型。

public class Notification
{
    public int NotificationID { get; set; }
    public bool ReadStatus { get; set; }
    public DateTime DateCreated { get; set; }
    [ForeignKey("User"), Column(Order=0)]
    public int UserID { get; set; }
    [ForeignKey("Actor"), Column(Order = 1)]
    public int ActorID { get; set; }

    [ForeignKey("NotificationType")]
    public int NotificationTypeID { get; set; }
    //public int ObjectID { get; set; }
    [ForeignKey("Comment")]
    public int? CommentID { get; set; }
    [ForeignKey("Project")]
    public int? ProjectID { get; set; }
    [ForeignKey("Book")]
    public int? BookID { get; set; }

    public Comment Comment { get; set; }
    public Book Book { get; set; }
    public Project Project { get; set; }
    public NotificationType NotificationType { get; set; }
    public User User { get; set; }
    public User Actor { get; set; }
}

任何给定的行将只有以下之一:BookID,, 我想获得总通知的计数,但我必须按这三个 ID 之一对它们进行分组ProjectID,其他两个 ID 为空。可能有 10 条评论通知,但其中 5 条用于回复,5 条用于其他操作 - 不过应该只有两条通知,而不是 10 条。CommentIDNotificationType

我将如何编写一个 lambda 表达式来获得这些计数?

4

1 回答 1

0

以下将起作用。

 var notificationCount = db.Notifications
      .GroupBy(x => new { x.CommentID, x.ProjectID, x.BookID, x.NotificationTypeID })
      .Count();

基于这个答案

于 2013-01-26T21:31:50.933 回答