linq 使用方法给对象赋值


不使用方法的赋值方式:

public UserAccountDTO GetUserAccountByEmployeeNo(string employeeNo)
        {
            if (!String.IsNullOrEmpty(employeeNo))
            {
                var query = (from ua in clientDbContext.UserAccounts
                             join e in clientDbContext.Employees
                             on ua.fkEmployee equals e.pkEmployee
                             where e.ID == employeeNo && (e.Status == null || e.Status != "Terminated")
                             select new UserAccountDTO()
                             {
                                 fkClient = ua.fkClient,
                                 pkUserAccount = ua.pkUserAccount,
                                 fkEmployee = ua.fkEmployee,
                                 UserName = ua.UserName,
                                 HashPassword = ua.HashPassword,
                                 SSOID = ua.SSOID,
                                 HasAcceptedTerms = ua.HasAcceptedTerms,
                                 IsAccountSuspended = ua.IsAccountSuspended,
                                 IsAccountLocked = ua.IsAccountLocked,
                                 AuthenticationMethod = ua.AuthenticationMethod,
                                 ID = e.ID,
                                 Role = ua.Role,
                                 InvalidAttempt = ua.InvalidAttempt,
                                 InvalidAttemptReset = ua.InvalidAttemptReset,
                                 LastPasswordChangedOn = ua.LastPasswordChangedOn,
                                 RequireChangePassword = ua.RequireChangePassword,
                                 ExpireOn = ua.ExpireOn,
                                 FullName = e.FullName,
                                 Email = e.Email,
                                 JobGrade = e.JobGrade,
                                 Avatar = ua.Avatar
                             });
                return query.FirstOrDefault();
            }
            return null;
        }

将赋值提取为方法的写法:

 public virtual UserAccountDTO GetUserAccountByUserName(string userName)
        {
            if (!String.IsNullOrEmpty(userName))
            {
                var query = (from ua in clientDbContext.UserAccounts
                             join e in clientDbContext.Employees
                                 on ua.fkEmployee equals e.pkEmployee
                             where ua.UserName == userName && (e.Status == null || e.Status != "Terminated")
                             select new
                             {
                                 ua,e
                             }).AsEnumerable().Select(x=>MappinpData(x.ua,x.e));//先选取为匿名类X,再传入方法
                return query.FirstOrDefault();
            }
            return null;
        }
        public UserAccountDTO MappinpData(UserAccount ua,Employee e)
        {
            return  new UserAccountDTO()
            {
                fkClient = ua.fkClient,
                pkUserAccount = ua.pkUserAccount,
                fkEmployee = ua.fkEmployee,
                UserName = ua.UserName,
                HashPassword = ua.HashPassword,
                SSOID = ua.SSOID,
                HasAcceptedTerms = ua.HasAcceptedTerms,
                IsAccountSuspended = ua.IsAccountSuspended,
                IsAccountLocked = ua.IsAccountLocked,
                AuthenticationMethod = ua.AuthenticationMethod,
                ID = e.ID,
                Role = ua.Role,
                InvalidAttempt = ua.InvalidAttempt,
                InvalidAttemptReset = ua.InvalidAttemptReset,
                LastPasswordChangedOn = ua.LastPasswordChangedOn,
                RequireChangePassword = ua.RequireChangePassword,
                ExpireOn = ua.ExpireOn,
                FullName = e.FullName,
                Email = e.Email,
                JobGrade = e.JobGrade,
                Avatar = ua.Avatar
            }; 
        }

 例子:

        public IEnumerable<Employee> GetEmployeeList(string searchKey)
        {
            var query = clientDbContext.Employees.Where(e => e.ID.Contains(searchKey) || e.FullName.Contains(searchKey)).AsEnumerable().Select(e => EntityCryption.Decrypt(e, entityCryptionKey));

            return query;
        }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM