Linq 实现两个对象实例List之间的赋值
1 public class UserCopy 2 { 3 public class LoginEntity 4 { 5 public string UserName { get; set; } 6 public string UserPwd { get; set; } 7 public DateTime ActiveTime { get; set; } 8 } 9 10 public class UserEntity : LoginEntity 11 { 12 public string UserID { get; set; } 13 public Int16 UserSex { get; set; } 14 public Int32 UserAge { get; set; } 15 } 16 17 protected void UserCopy() 18 { 19 List<UserEntity> userList = new List<UserEntity>() { 20 new UserEntity(){ 21 UserID="U1", 22 UserName="U1", 23 UserPwd="U1PWD", 24 ActiveTime=DateTime.Now 25 }, 26 new UserEntity(){ 27 UserID="U2", 28 UserName="U2", 29 UserPwd="U2PWD", 30 ActiveTime=DateTime.Now}, 31 new UserEntity(){ 32 UserID="U2", 33 UserName="U2", 34 UserPwd="U2PWD", 35 ActiveTime=DateTime.Now} 36 }; 37 38 List<LoginEntity> loginList = new List<LoginEntity>(); 39 //关键代码:遍历赋值 40 userList.ForEach(user => loginList.Add(new LoginEntity() 41 { 42 UserName = user.UserName, 43 UserPwd = user.UserPwd, 44 ActiveTime = user.ActiveTime 45 })); 46 } 47 }