2011-12-29 70 views
0
public List<RoleAssignment> lstRoleAssignments1 = new List<RoleAssignment>(); 
public List<RoleAssignment> lstRoleAssignments2 = new List<RoleAssignment>(); 


public class RoleAssignment 
{ 
int RoleAssignmentId{get;set;} 
} 

RoleAssignment objRoleAssign = new RoleAssignment(); 
objRoleAssign.RoleAssignmentId = 1; 

lstRoleAssignments1.Add(objROleAssign); 

objRoleAssign.RoleAssignmentId = 2; 
lstRoleAssignments2.Add(objROleAssign); 

這兩個列表必須具有該屬性RoleAssignmentId一個對象和值是在兩種情況下。添加由值對象到一個列表(這不是隻是一個參考)

我想要的是,lstRoleAssignments1中的RoleAssignmentId是1,而lstRoleAssignments2是2.我不能使用結構,因爲我需要使用list作爲屬性,因此需要在類中實例化它。我怎樣才能做到這一點?

回答

1

創建RoleAssignment

RoleAssignment objRoleAssign = new RoleAssignment(); 
objRoleAssign.RoleAssignmentId = 1;  
lstRoleAssignments1.Add(objROleAssign); 

objRoleAssign = new RoleAssignment(); 
objRoleAssign.RoleAssignmentId = 2; 
lstRoleAssignments2.Add(objROleAssign); 

的新實例
lstRoleAssignments1.Add(new RoleAssignment { RoleAssignmentId = 1 }); 
lstRoleAssignments2.Add(new RoleAssignment { RoleAssignmentId = 2 }); 
+0

剛剛輸入了相同的答案時,這都挺過來了! – Geoff 2011-12-29 13:39:06

+0

是的,我們可以做到這一點,但是將對象添加到列表中會進入for循環,換句話說20000次,我不想創建這些許多對象。 – Anshul 2011-12-29 14:00:44

+0

@devdigital'RoleAssignment objRoleAssign = new RoleAssignment(); for(int i = 0; i <20000; i ++){ob​​jRoleAssign.RoleAssignmentId = i; lstRoleAssignments1.Add(objROleAssign); objRoleAssign.RoleAssignmentId = i + 1; lstRoleAssignments2.Add(objROleAssign); }' – Anshul 2011-12-29 14:11:35

相關問題