在C#中创建列表并将值添加到列表 [英] Create List in C# and add values to list
本文介绍了在C#中创建列表并将值添加到列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public class DashInfo
{
public int Gid { get ; set ; }
public string 组{ get 跨度>; set ; }
public string Region { get 跨度>; set ; }
public int 持续时间{ get 跨度>; set ; }
}
private void OnLoadDashBoardDataCompleted(LoadOperation< DashboardJson> loadOperation)
{
foreach (DashboardJson dashjson in loadOperation.Entities)
{
if (dashjson.groups.ElementAt( 0 )。Cells.ElementAt( 0 )!= null )
{
foreach ( var region in loadOperation.Entities)
{
foreach ( var group < span class =code-keyword> in region.groups)
{
List< DashInfo> dashinfos = new 列表< DashInfo>();
{
new DashInfo {Gid = group .gid,Group = group .Name,Region = group .Region,Duration = group .AvgInf};
}
}
}
}
}
}
此方法是真还是假?
如果为false则意味着请帮助我..如何将Gid,Name,Region和Duartion添加到列表中..
解决方案
做出一些改变,比如
private void OnLoadDashBoardDataCompleted(LoadOperation< dashboardjson> loadOperation)
{
List< dashinfo> dashinfos = new 列表< dashinfo>();
foreach (DashboardJson dashjson in loadOperation.Entities)
{
if (dashjson.groups.ElementAt( 0 )。Cells.ElementAt( 0 )!= null )
{
foreach ( var region in loadOperation.Entities)
{
foreach ( var group in region.groups)
{
{
dashinfos。 add ( new DashInfo {Gid = group .gid,Group = group .Name,Region = group .Region,Duration = group .AvgInf});
}
}
}
}
}
}
你是每次要添加新项目时创建新列表。此外,在此之后你没有对列表做任何事情。我想你要填写列表,然后从方法中返回:
private 列表< dashinfo> ; OnLoadDashBoardDataCompleted(LoadOperation< dashboardjson> loadOperation)
{
List< dashinfo> dashinfos = new 列表< dashinfo>(); // 创建一次列表
foreach (DashboardJson dashjson in loadOperation.Entities)
{
if (dashjson.groups.ElementAt( 0 )。Cells.ElementAt( 0 )!= < span class =code-keyword> null )
{
foreach ( var region in loadOperation.Entities)
{
foreach ( var group region.groups)
{
// 将项目添加到列表中
dashinfos.Add( new DashInfo {Gid = group .gid,Group = group .Name,Region = group .Region,Duration = group .AvgInf });
}
}
}
}
}
return dashinfos; // 将创建的列表返回给调用者
}
public class DashInfo
{
public int Gid { get; set; }
public string Group { get; set; }
public string Region { get; set; }
public int Duration { get; set; }
}
private void OnLoadDashBoardDataCompleted(LoadOperation<DashboardJson> loadOperation)
{
foreach (DashboardJson dashjson in loadOperation.Entities)
{
if (dashjson.groups.ElementAt(0).Cells.ElementAt(0) != null)
{
foreach (var region in loadOperation.Entities)
{
foreach (var group in region.groups)
{
List<DashInfo> dashinfos = new List<DashInfo>();
{
new DashInfo { Gid = group.gid, Group = group.Name, Region = group.Region, Duration = group.AvgInf };
}
}
}
}
}
}
This method is true or false?
If false means pls help me.. How to add Gid,Name,Region, and Duartion to list..
解决方案
Make Some Change like
private void OnLoadDashBoardDataCompleted(LoadOperation<dashboardjson> loadOperation) { List<dashinfo> dashinfos = new List<dashinfo>(); foreach (DashboardJson dashjson in loadOperation.Entities) { if (dashjson.groups.ElementAt(0).Cells.ElementAt(0) != null) { foreach (var region in loadOperation.Entities) { foreach (var group in region.groups) { { dashinfos.add(new DashInfo { Gid = group.gid, Group = group.Name, Region = group.Region, Duration = group.AvgInf }); } } } } } }
You are creating a new list each time you want to add a new item. Furthermore you are not doing anything with the list after that. I guess you want to fill the list and then return it form the method:
private List<dashinfo> OnLoadDashBoardDataCompleted(LoadOperation<dashboardjson> loadOperation) { List<dashinfo> dashinfos = new List<dashinfo>(); // create the list once foreach (DashboardJson dashjson in loadOperation.Entities) { if (dashjson.groups.ElementAt(0).Cells.ElementAt(0) != null) { foreach (var region in loadOperation.Entities) { foreach (var group in region.groups) { // add items to the list dashinfos.Add(new DashInfo { Gid = group.gid, Group = group.Name, Region = group.Region, Duration = group.AvgInf }); } } } } } return dashinfos; // return the created list to the caller }
这篇关于在C#中创建列表并将值添加到列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文