public IResponseDTO GetCustomerByWorkShopId(Guid id) { try {//GetCustomerByWorkShopId var Chats = _ChatRepositroy.Filter(x => x.WorkShopId == id).ToList(); List <ResponseChat> Wrokshops = new List <ResponseChat>(); foreach (var customerChat in Chats) { if (customerChat.CustomerId != null) { var customerHasChat = _CustomerRepositroy.Find(customerChat.CustomerId); var chat = new ResponseChat(); chat.Content = customerChat.Content; chat.CreationDate = customerChat.CreationDate; chat.CountUnRead = _ChatRepositroy.Get(x => x.WorkShopId == id && x.IsRead == false).Count(); chat.CustomerName = customerHasChat.Name; chat.ImageUrl = customerHasChat.ImageUrl; chat.CustomerId = customerHasChat.CustomerId; Wrokshops.Add(chat); } } var result = Wrokshops.GroupBy(x => x.CustomerId) .Select(x => x.OrderByDescending(y => y.CreationDate).First()); _response.Data = result.Select(x => new { Content = x.Content, CountUnRead = x.CountUnRead, CreationDate = x.CreationDate, CustomerName = x.CustomerName, CustomerId = x.CustomerId, ImageUrl = x.ImageUrl, }); _response.IsPassed = true; _response.Message = "Done"; } catch (Exception ex) { _response.Data = null; _response.IsPassed = false; _response.Message = "Error " + string.Format("{0} - {1} ", ex.Message, ex.InnerException != null ? ex.InnerException.FullMessage() : ""); } return(_response); }