public IEnumerable <ResourceUserRightHistoryModel> GetUserRightsHistoryOnDate(DateTime from, DateTime to, int idRequestUser) { var userRights = _rightRepository.GetRequestUserRightAssocs() .Include(r => r.RequestUserAssoc) .Include(r => r.RequestUserAssoc.Request) .Include(r => r.RequestUserAssoc.Request.RequestStates) .Include(r => r.RequestUserAssoc.RequestUser) .Include(r => r.ResourceRight) .Include(r => r.RequestRightGrantType) .Include(r => r.ResourceRight.Resource) .Include(r => r.RequestUserAssoc.Request.User) .Include(r => r.RequestUserAssoc.Request.User.Department) .Where(r => r.RequestUserAssoc.IdRequestUser == idRequestUser && !r.RequestUserAssoc.Deleted && !r.RequestUserAssoc.Request.Deleted && r.RequestUserAssoc.Request.IdCurrentRequestStateType == 4 && r.RequestUserAssoc.Request.IdRequestType != 4 && r.RequestUserAssoc.Request.IdRequestType != 3 && r.RequestUserAssoc.Request.CurrentRequestStateDate >= from && r.RequestUserAssoc.Request.CurrentRequestStateDate <= to) .Select(r => new ResourceUserRightHistoryModel { IdRequest = r.RequestUserAssoc.IdRequest, IdRequestType = r.RequestUserAssoc.Request.IdRequestType, RequestCompleteDate = r.RequestUserAssoc.Request.CurrentRequestStateDate.Value, DelegationExtInfo = null, AclUser = r.RequestUserAssoc.Request.User, RequestUser = r.RequestUserAssoc.RequestUser, GrantType = r.RequestRightGrantType, Right = r.ResourceRight, RequestRightDescription = r.Descirption }).ToList(); var delegations = (from delegationRow in _rightRepository.GetDelegationRequestUsersExtInfo() .Include(r => r.DelegateToUser) join userAssocRow in _rightRepository.GetRequestUserAssocs() .Include(r => r.Request) .Include(r => r.Request.RequestStates) .Include(r => r.Request.User) .Include(r => r.Request.User.Department) .Include(r => r.RequestUser) on delegationRow.IdRequestUserAssoc equals userAssocRow.IdRequestUserAssoc join userRightAssocRow in _rightRepository.GetRequestUserRightAssocs() .Include(r => r.RequestRightGrantType) .Include(r => r.ResourceRight) .Include(r => r.ResourceRight.Resource) on userAssocRow.IdRequestUserAssoc equals userRightAssocRow.IdRequestUserAssoc where delegationRow.IdDelegateToUser == idRequestUser || userAssocRow.IdRequestUser == idRequestUser && userAssocRow.Request.IdCurrentRequestStateType == 4 && !delegationRow.Deleted && !userAssocRow.Deleted && !userAssocRow.Request.Deleted && !userRightAssocRow.Deleted select new ResourceUserRightHistoryModel { IdRequest = userAssocRow.IdRequest, IdRequestType = userAssocRow.Request.IdRequestType, RequestCompleteDate = userAssocRow.Request.CurrentRequestStateDate.Value, DelegationExtInfo = delegationRow, AclUser = userAssocRow.Request.User, RequestUser = userAssocRow.RequestUser, GrantType = userRightAssocRow.RequestRightGrantType, Right = userRightAssocRow.ResourceRight, RequestRightDescription = userRightAssocRow.Descirption }).ToList(); var excludeUser = (from requestRow in _rightRepository.GetRequests() join userAssocRow in _rightRepository.GetRequestUserAssocs() on requestRow.IdRequest equals userAssocRow.IdRequest where requestRow.IdCurrentRequestStateType == 4 && userAssocRow.IdRequestUser == idRequestUser && requestRow.IdRequestType == 3 select new ResourceUserRightHistoryModel { IdRequest = requestRow.IdRequest, IdRequestType = requestRow.IdRequestType, RequestCompleteDate = requestRow.CurrentRequestStateDate.Value, AclUser = requestRow.User }).ToList(); return(userRights.Union(delegations).Union(excludeUser).OrderBy(r => r.RequestCompleteDate)); }