Monday 20 April 2015

Group by and Count in Entity Framework

Group and Count in Entity Framework


Group and Count in Entity Framework


DbContext Db = new DbContext();



var messageList =
 from UM in Db.userMessages.ToList()
                              where (UM.fromUserId == toMailId
                            || UM.toUserId == toMailId) 
                              group UM by new {UM.fromUserId,UM.toUserId} into m 
                              select new userMsgGroup
                              { 
                                fromUserId = m.Key.fromUserId,
                                toUserId = m.Key.toUserId,
                                msgCount = m.Count()
                                                              };
List<userMsgGroup> msgGroup = new List<userMsgGroup>();
            msgGroup = messageList.ToList();

 public class userMsgGroup
{
public long fromUserId { get; set; }
public long toUserId { get; set; }
public int msgCount { get;set;}
}



No comments:

Post a Comment