我刚刚问了一个关于我的班级的问题:
Public class Parent {
public IList<ParentDetail> ParentDetails {
get { return _ParentDetails; }
}
private List<ParentDetail> _ParentDetails = new List<ParentDetail>();
public Parent() {
this._ParentDetails = new List<ParentDetail>();
}
}
public class ParentDetail {
public int Id { get; set; }
}
}
这里的一个专家(Jon)告诉我如何按照ParentDetails.Id的顺序迭代遍历这个类。以下是他的解决方案,非常好用。 之前的问题
foreach(var details in Model.Parent.ParentDetails.OrderBy(d => d.Id))
{
// details are processed in increasing order of Id here
// what's needed is to get access to the original order
// information. Something like as follows:
// select index position from ParentDetails where Id = details.ID
}
我还需要在foreach循环中显示与Id对应的列表索引值以及ParentDetail类中的其他数据。
例如,在“// details are processed”处,我希望能够打印出与foreach循环中当前Id对应的索引值。