public BasketViewModel MapFrom(BasketEntity basketEntity) { var itemViewModels = new List <ItemViewModel>(); var items = basketEntity.Items.GroupBy(x => x.ItemId); foreach (var item in items) { var itemViewModel = _itemMapper.MapFrom(item.First()); itemViewModel.Quantity = item.Count(); itemViewModels.Add(itemViewModel); } return(new BasketViewModel { BasketId = basketEntity.BasketId, Token = basketEntity.Token, Items = itemViewModels, Total = basketEntity.Items.Sum(x => x.Cost) }); }