public IEnumerable <SYS_AuthorizationDTO> GetAuthorizationUser(string userId, byte companyId, byte applicationId) { IUnitOfWork unitOfWork = _UserRepository.UnitOfWork; List <SYS_AuthorizationDTO> lst = null; try { var lstOptions = _OptionRepository.FindBy(opt => opt.ApplicationId == applicationId && opt.Active); var lstAuths = _UserRepository.GetAuthorizationUser(userId, companyId, applicationId); List <SYS_AuthorizationDTO> lstAuthorization = new List <SYS_AuthorizationDTO>(); var entities = (from opt in lstOptions join auth in lstAuths on opt.OptionId equals auth.OptionId into records from r in records.DefaultIfEmpty() select new { options = opt, authorization = r }).ToList(); foreach (var item in entities) { var detail = new List <SYS_DetailAuthorizationDTO>(); foreach (var d in item.options.SYS_DetailOption) { SYS_DetailAuthorization detailAuthorization = null; if (item.authorization != null) { detailAuthorization = item.authorization.SYS_DetailAuthorization.Where(c => c.DetailOptionId == d.DetailOptionId).FirstOrDefault(); } detail.Add(new SYS_DetailAuthorizationDTO() { UserId = userId, CompanyId = companyId, OptionId = item.options.OptionId, DetailOptionId = d.DetailOptionId, StartDate = (detailAuthorization == null ? new DateTime?() : detailAuthorization.StartDate), EndDate = (detailAuthorization == null ? new DateTime?() : detailAuthorization.EndDate), DetailName = d.Name, Allowed = (detailAuthorization == null ? false : true) }); } lstAuthorization.Add(new SYS_AuthorizationDTO() { UserId = userId, CompanyId = companyId, OptionId = item.options.OptionId, StartDate = (item.authorization == null ? new DateTime?() : item.authorization.StartDate.ToUniversalTime()), EndDate = (item.authorization == null ? new DateTime?() : item.authorization.EndDate.ToUniversalTime()), ParentId = item.options.ParentId, OptionName = item.options.Name, Icon = item.options.Icon, Leaf = item.options.Leaf, Allowed = (item.authorization == null ? false : true), SYS_DetailAuthorization = detail }); } lst = lstAuthorization; } catch (Exception ex) { _log.Error(ex.Message, ex.Source, ex.StackTrace); } return(lst); }