我有一个DateTime
的集合,名为reportLogs
。我需要从Collection<DateTime>
创建一个Collection<T>
的ShortDateString
。什么是最有效的方法呢?将一个集合转换为另一个集合(不是列表)
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);
}
EDIT:
的问题是关于Collection of string
;而不是约List of string
。我们如何处理字符串的集合?
参考:
- Using LINQ to convert List<U> to List<T>
- LINQ convert DateTime to string
- Convert a datetime in a subcollection of collection and use it in LINQ to SQL
- convert Collection<MyType> to Collection<Object>
的问题是关于 '串集';而不是'字符串列表'。我们如何处理'字符串集合'? – Lijo
@Lijo - ['List''继承'ICollection '](http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx) - 表示'List ** ** IS ** a Collection ! –
Jamiec
@Lijo - 也'集合'有一个构造函数,它需要'IList ' - 所以你可以随时将列表传递给新的Collection的结果。我会用这个更新答案。 –
Jamiec