使用for循环添加IList

时间:2013-02-25 11:23:52

标签: c# generics for-loop

我有结果列表

var attributeresult= " some list of items......";

现在我正在尝试循环表单结果并添加到IList.but我只是最后插入值,但我想获得所有值。

 IList<DynamicColumn> idynamicttableColumns = new List<DynamicColumn>();
 DynamicColumn dynamictableColumns = new DynamicColumn();


 for (int i = 0; i < attributeresult.Count(); i++)
                        {
                            dynamictableColumns.Name = attributeresult.ElementAt(i).AttributeName;
                            dynamictableColumns.Type = attributeresult.ElementAt(i).AttributeSqlType;
                            dynamictableColumns.IsNullable = false;
                            idynamicttableColumns.Add(dynamictableColumns);

                        }

我必须完成for循环,而不是每个循环。

1 个答案:

答案 0 :(得分:3)

DynamicColumn dynamictableColumns = new DynamicColumn();移到您的循环中:

IList<DynamicColumn> idynamicttableColumns = new List<DynamicColumn>();
int count = attributeresult.Count();

for (int i = 0; i < count; i++)
{
    var item = attributeresult.ElementAt(i);
    DynamicColumn dynamictableColumns = new DynamicColumn();
    dynamictableColumns.Name = item .AttributeName;
    dynamictableColumns.Type = item .AttributeSqlType;
    dynamictableColumns.IsNullable = false;
    idynamicttableColumns.Add(dynamictableColumns);
}