0

我有一个界面,例如

public interface Details
{
    int Id { get; set; }
    string Name { get; set; } 
}        

我在另一个类中有一个方法,比如

public void GetDetails()      
{ 
    List<Details> lstDetails = new List<Details>();        
    DetailsImpl objDetailsImpl;
    Details objDetails = new DetailsImpl();
    for (int i = 1; i <= 2; i++)
    {
        objDetailsImpl = new DetailsImpl(); //The **DetailsImpl** class is given below
        objDetails.Id = i;
        if(i==1)
           objDetails.Name = "FirstName";
        else
           objDetails.Name = "SecondName";
        lstDetails.Add(objDetails);
    }
    objDetailsImpl.InsertRecords(lstDetails);  
}

我在另一个派生类中编写InsertRecords方法,例如,

public class DetailsImpl : Details
{
    Dictionary<object, object> objDictionary = new Dictionary<object, object>();
    public string Name { get; set; }
    public int Id { get; set; }

    public int InsertRecords(List<Details> lstDetails)
    {
        for (int i = 0; i < lstDetails.Count; i++)
        {
            //i want to get the integer value "Id" from List(lstDetails)
            Id = //How to get the Id value based on count, now the <lstDetails> count is 2

            //i want to get the Name object value also, how to i get???
            Name = Convert.ToString(String.Join(",",lstDetails.Select(s => s.Name).ToArray()));
            //If i using the above line for getting **Name** value means, its comes like "FirstName,LastName", but i want **Name** value based on row.....


            objDictionary.Clear();
            objDictionary.Add("@Id",Id);
            objDictionary.Add("@Id",Name);
            //Here i passed this parameter to "ExecuteNonQry" method for insert operations
            ExecuteNonQry("sp_name",objDictonary,CommandType.StoredProcedure);
        }
        return 1;
    }
}

我想根据列表计数获取Id、Name对象值。我该如何解决这个问题?

4

3 回答 3

4

你正在尝试写

String.Join(", ", list.Select(o => o.Name))
于 2012-05-23T14:51:55.810 回答
4

第th 元素的Name性质是i

lstDetails[i].Name

值得指出的是,你没有aList<>你有List<T>- 而且你没有告诉我们 theT是什么。

于 2012-05-23T14:52:26.220 回答
1

假设您的 List 有多个 Detail 类型的对象,其中 Detail 如下所示:

public class Detail
{
    public int Id{get;set;}
    public string Name {get;set;}
}

现在你有一个包含 2 个细节对象的列表

lstDetails.Add(new Detail(){Id = 1, Name = "John"});
lstDetails.Add(new Detail(){Id = 2, Name = "Charlie"});

现在,如果您想在循环完成后strValue包含John, Charlie,您可以使用下面的代码而不使用循环

strValue = String.Join(",", lstDetails.Select(s => s.Name).ToArray());
于 2012-05-23T15:10:57.840 回答