English 中文(简体)
C# - 如何在基于列表计数的环环中获取 List 对象值
原标题:C# - How to get List<> object values within the for loop based on List count
  • 时间:2012-05-23 14:50:28
  •  标签:
  • c#
  • asp.net

我有一个界面喜欢,

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 元素的属性 >

lstDetails[i].Name

值得指出的是,您确实有 not 的有 List<> 您有 List <T> - 您没有告诉我们 T 是什么。

问题回答

您正在试图写入

String.Join(", ", list.Select(o => o.Name))

假设您的列表有多个详细内容类型对象,详细内容如下:

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

现在你有了一份列表,其中有两个细节对象

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

如果您现在想要 strvaluue 包含 John, Charlie 一旦循环完成, 您可以使用下面的代码而无需循环

strValue = String.Join(",", lstDetails.Select(s => s.Name).ToArray());




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!