private void BindGrid() { using ( new UnitOfWorkScope() ) { var communications = new CommunicationService() .Queryable() .Where( c => c.Status != CommunicationStatus.Transient ); string subject = rFilter.GetUserPreference( "Subject" ); if ( !string.IsNullOrWhiteSpace( subject ) ) { communications = communications.Where( c => c.Subject.StartsWith( subject ) ); } Guid entityTypeGuid = Guid.Empty; if ( Guid.TryParse( rFilter.GetUserPreference( "Channel" ), out entityTypeGuid ) ) { communications = communications.Where( c => c.ChannelEntityType != null && c.ChannelEntityType.Guid.Equals( entityTypeGuid ) ); } string status = rFilter.GetUserPreference( "Status" ); if ( !string.IsNullOrWhiteSpace( status ) ) { var communicationStatus = (CommunicationStatus)System.Enum.Parse( typeof( CommunicationStatus ), status ); communications = communications.Where( c => c.Status == communicationStatus ); } if ( canApprove ) { int personId = 0; if ( int.TryParse( rFilter.GetUserPreference( "Created By" ), out personId ) && personId != 0 ) { communications = communications.Where( c => c.SenderPersonId.HasValue && c.SenderPersonId.Value == personId ); } } else { communications = communications.Where( c => c.SenderPersonId.HasValue && c.SenderPersonId.Value == CurrentPersonId ); } string content = rFilter.GetUserPreference( "Content" ); if ( !string.IsNullOrWhiteSpace( content ) ) { communications = communications.Where( c => c.ChannelDataJson.Contains( content ) ); } var recipients = new CommunicationRecipientService().Queryable(); var sortProperty = gCommunication.SortProperty; var queryable = communications .Join( recipients, c => c.Id, r => r.CommunicationId, ( c, r ) => new { c, r } ) .GroupBy( cr => cr.c ) .Select( g => new CommunicationItem { Id = g.Key.Id, Communication = g.Key, Recipients = g.Count(), PendingRecipients = g.Count( s => s.r.Status == CommunicationRecipientStatus.Pending ), SuccessRecipients = g.Count( s => s.r.Status == CommunicationRecipientStatus.Success ), FailedRecipients = g.Count( s => s.r.Status == CommunicationRecipientStatus.Failed ), CancelledRecipients = g.Count( s => s.r.Status == CommunicationRecipientStatus.Cancelled ) } ); if ( sortProperty != null ) { queryable = queryable.Sort( sortProperty ); } else { queryable = queryable.OrderByDescending( c => c.Communication.Id ); } gCommunication.DataSource = queryable.ToList(); gCommunication.DataBind(); } }