C# 列表<;实体>;问题
我有这种代码C# 列表<;实体>;问题,c#,C#,我有这种代码 foreach(GridViewRow row in gvTagDeductionList.Rows) { DeductionEntity de = new DeductionEntity(); //modify this condition, make cell number to be more dynamic if (((CheckBox)row.Cells[2].FindControl("chkInclude")).Checked == true)
foreach(GridViewRow row in gvTagDeductionList.Rows)
{
DeductionEntity de = new DeductionEntity();
//modify this condition, make cell number to be more dynamic
if (((CheckBox)row.Cells[2].FindControl("chkInclude")).Checked == true)
{
//following id's are said to be in numeric format in the first place
//ede.EmployeeID = Convert.ToInt32(HttpContext.Current.Session["_newEmpID"].ToString());
ede.DeductionID = Convert.ToInt32((((Label)row.Cells[0].FindControl("lblID")).Text).ToString());
ede.CreatedBy_UserID = Convert.ToInt32(HttpContext.Current.Session["_employeeID"].ToString());
ede.UpdatedBy_UserID = Convert.ToInt32(HttpContext.Current.Session["_employeeID"].ToString());
de = e201u.GetDeductionDetails(ede.DeductionID);
e201u.InsertEmployeeDeduction(ede);
lstEntity.Add(de);
}
}
de = e201u.GetDeductionDetails(ede.DeductionID);
在循环中第二次遇到此代码后,此代码中的第一条记录
lstenty.Add(de)代码>将被修改,最后我将在我的列表中得到两个条目,这是通过de=e201u.getDeclarationDetails(ede.DeclarationId)得到的最后一个实体代码>在循环中,必须再次创建插入列表的对象。现在您只需插入它,然后更改它并再次插入。仅将对对象的引用保存到列表中,而不是实际对象本身。作为循环的第一行(尚未显示),请尝试根据ede对象的类型编写ede=new…()
对于所有基于引用的类型(类),都会出现同样的“问题”,但对于基于值的类型(结构),则不会出现同样的问题,因为对于结构,您会将对象的实际副本保存到列表中,而不仅仅是像对类那样的引用
这里有一个例子
public class A
{
public int Test{ get; set; }
}
代码中的其他位置:
List<A> list = new List<A>();
A a = new A();
for( int i = 0; i < 2; i++ )
{
a.Test = i;
list.Add(a);
}
List<A> list = new List<A>();
for( int i = 0; i < 2; i++ )
{
A a = new A();
a.Test = i;
list.Add(a);
}
List List=新列表();
A=新的A();
对于(int i=0;i<2;i++)
{
a、 试验=i;
列表.添加(a);
}
这将得到一个列表,其中包含对同一对象的两个引用。因此,列表[0]。测试为1,列表[1]。测试为1
代码中的其他位置:
List<A> list = new List<A>();
A a = new A();
for( int i = 0; i < 2; i++ )
{
a.Test = i;
list.Add(a);
}
List<A> list = new List<A>();
for( int i = 0; i < 2; i++ )
{
A a = new A();
a.Test = i;
list.Add(a);
}
List List=新列表();
对于(int i=0;i<2;i++)
{
A=新的A();
a、 试验=i;
列表.添加(a);
}
这将得到一个列表,其中包含对两个不同a对象的引用。所以列表[0]。测试为0,列表[1]。测试为1 在循环内部,您必须创建再次插入列表的对象。现在您只需插入它,然后更改它并再次插入。仅将对对象的引用保存到列表中,而不是实际对象本身。作为循环的第一行(尚未显示),请尝试根据ede对象的类型编写ede=new…()
对于所有基于引用的类型(类),都会出现同样的“问题”,但对于基于值的类型(结构),则不会出现同样的问题,因为对于结构,您会将对象的实际副本保存到列表中,而不仅仅是像对类那样的引用
这里有一个例子
public class A
{
public int Test{ get; set; }
}
代码中的其他位置:
List<A> list = new List<A>();
A a = new A();
for( int i = 0; i < 2; i++ )
{
a.Test = i;
list.Add(a);
}
List<A> list = new List<A>();
for( int i = 0; i < 2; i++ )
{
A a = new A();
a.Test = i;
list.Add(a);
}
List List=新列表();
A=新的A();
对于(int i=0;i<2;i++)
{
a、 试验=i;
列表.添加(a);
}
这将得到一个列表,其中包含对同一对象的两个引用。因此,列表[0]。测试为1,列表[1]。测试为1
代码中的其他位置:
List<A> list = new List<A>();
A a = new A();
for( int i = 0; i < 2; i++ )
{
a.Test = i;
list.Add(a);
}
List<A> list = new List<A>();
for( int i = 0; i < 2; i++ )
{
A a = new A();
a.Test = i;
list.Add(a);
}
List List=新列表();
对于(int i=0;i<2;i++)
{
A=新的A();
a、 试验=i;
列表.添加(a);
}
这将得到一个列表,其中包含对两个不同a对象的引用。所以列表[0]。测试为0,列表[1]。测试为1 如果您所做的只是将其分配给e201u,则无需创建新的演绎属性。您应该使用事件,而不是每次发生事件时都遍历整个数据网格。只要我的2美分。如果您所做的只是将其分配给e201u,则无需创建新的演绎实体。GetDecreditionDetails
您应该使用事件,而不是每次发生事件都遍历整个数据网格。只要我的2美分。