我有这样的课程
public class Sample
{
public string A { get; set; }
public int B { get; set; }
}
var sampleValues = new List<Sample>(){
new Sample(){ A = "a1", B = 1 },
new Sample(){ A = "a2", B = 2 },
new Sample(){ A = "a3", B = 3 },
new Sample(){ A = "a4", B = 4 },
.....
new Sample(){ A ="a30", B = 30}
};
有没有简单的方法用30个连续值初始化它,如图所示?
答案 0 :(得分:12)
使用Enumerable.Range
之类的:
List<Sample> samvalues = Enumerable.Range(1, 30)
.Select(r => new Sample {A = "a" + r, B = r})
.ToList();
或者
List<Sample> samvalues = Enumerable.Range(1, 30)
.Select(r => new Sample {A = String.Format("{0}{1}", "a", r), B = r})
.ToList();