public class Comment
{
public int IndexNo {get;set;}
public DateTime CreatedOn {get;set;}
}
static void Main()
{
int i = 0;
var comments = new List<Comment>()
{
new Comment() { CreatedOn = DateTime.Now.AddMinutes(1) },
new Comment() { CreatedOn = DateTime.Now.AddMinutes(2) },
new Comment() { CreatedOn = DateTime.Now.AddMinutes(3) },
new Comment() { CreatedOn = DateTime.Now.AddMinutes(4) },
};
// Not very nice solution..
var foos = new List<Comment>();
foreach(var foo in comments.orderby(c=> c.createdOn))
{
foo.IndexNo = ++i;
foos.add(foo);
}
}
リストから IndexNo プロパティに増分番号を割り当てるにはどうすればよいですか? 私の予想される出力は次のとおりです。
- 2004年4月15日 午後2時37分~1
- 2004年4月15日 午後2時38分~2
- 2004年4月15日 午後2時39分~3時
- 2004年4月15日 午後2時40分~4時
ありがとう。