0

DateTimenamedのコレクションがありreportLogsます。this からCollection<T>ofを作成する必要があります。それを行う最も効率的な方法は何ですか?ShortDateStringCollection<DateTime>

Collection<DateTime> reportLogs =  reportBL.GetReportLogs(1, null, null);
Collection<string> logDates = new Collection<string>();
foreach (DateTime log in reportLogs)
{
    string sentDate = log.ToShortDateString();
    logDates.Add(sentDate);
}

編集

質問はCollection of string;についてです。ついておりませんList of string。string の Collection をどのように処理できますか?

参考

  1. LINQ を使用して List<U> を List<T> に変換する
  2. LINQはDateTimeを文字列に変換します
  3. コレクションのサブコレクションで日時を変換し、LINQ to SQL で使用する
  4. Collection<MyType> を Collection<Object> に変換します
4

3 に答える 3

3

あなたがちょうど満足しているならIEnumerable<string>

IEnumerable<string> logDates = reportBL.GetReportLogs(1, null, null)
                                      .Select(d => d.ToShortDateString());

もう 1 回呼び出すだけで、これをList<string>簡単に変更できます

List<string> logDates = reportBL.GetReportLogs(1, null, null)
                                      .Select(d => d.ToShortDateString())
                                      .ToList();

編集:オブジェクトが本当に必要な場合Collection<T>は、そのクラスにはコンストラクターがありIList<T>、次のように動作します:

Collection<string> logDates = new Collection(reportBL.GetReportLogs(1, null, null)
                                      .Select(d => d.ToShortDateString())
                                      .ToList());
于 2012-12-03T10:11:28.940 に答える
0
var logDates= reportLogs.Select(d => d.ToShortDateString());

オプションで、.ToList()

于 2012-12-03T10:13:30.127 に答える