0

I have a collection of DateTime named reportLogs. I need to create a Collection<T> of ShortDateString from this Collection<DateTime>. What is the most efficient way to do it?

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:

The question is about Collection of string; not about List of string. How can we handle the Collection of string ?

REFERENCE:

  1. Using LINQ to convert List<U> to List<T>
  2. LINQ convert DateTime to string
  3. Convert a datetime in a subcollection of collection and use it in LINQ to SQL
  4. convert Collection<MyType> to Collection<Object>
Community
  • 1
  • 1
LCJ
  • 22,196
  • 67
  • 260
  • 418

3 Answers3

3

If you're happy with just IEnumerable<string>:

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

You could turn this to List<string> easily with 1 more call

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

Edit: If you really need your object to be Collection<T> then that class has a constructor which takes IList<T> so the following will work:

Collection<string> logDates = new Collection(reportBL.GetReportLogs(1, null, null)
                                      .Select(d => d.ToShortDateString())
                                      .ToList());
Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • 1
    The question is about 'Collection of string'; not about 'List of string'. How can we handle the 'Collection of string' ? – LCJ Dec 06 '12 at 12:07
  • @Lijo - [`List` inherits `ICollection`](http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx) - which means `List` **IS** a Collection! – Jamiec Dec 06 '12 at 12:13
  • @Lijo - Also `Collection` has a constructor which takes `IList` - so you can always pass the list to the result of the above to a new Collection. I'll update the answer with this. – Jamiec Dec 06 '12 at 12:16
  • 1
    @Lijo - I struggle to think of a better way. Whatever you do, you will need to create a new instance of `Collection` and fill it. This solution just does that bit by augmenting the original Collection – Jamiec Dec 06 '12 at 12:18
  • For reference: http://stackoverflow.com/questions/398903/what-is-the-difference-between-list-of-t-and-collectionof-t – Jamiec Dec 06 '12 at 12:20
  • @Jamiec *List inherits ICollection - which means List IS a Collection*. No, Collection is different from `ICollection` – nawfal May 16 '13 at 11:13
  • @nawfal - I fail to see your point. The fact that `List` implements `ICollection` means that for all intents and purposes you can treat a list as a collection. That was the only point I was making. – Jamiec May 16 '13 at 11:22
  • 1
    @Jamiec the point you failed to see has been incorporated in your edited answer, hope its clear now :) When someone says *The question is about 'Collection of string'; not about 'List of string'*, I hope its clear for someone as experienced as you that the guy is talking about `Collection` and not about the generic term *collection*, especially given the code already. – nawfal May 16 '13 at 12:09
0
var logDates= reportLogs.Select(d => d.ToShortDateString());

Optionally you can add a .ToList()

Rik
  • 28,507
  • 14
  • 48
  • 67
0
 //Create a collection of DateTime 

DateTime obj =new DateTime(2013,5,5);

List<DateTime>lstOfDateTime = new List<DateTime>()
{
  obj,obj.AddDays(1),obj.AddDays(2)


};

use List class convertAll Method to convert to ShortDateString

//Convert to ShortDateString

   Lis<string> toShortDateString =  lstOfDateTime.ConvertAll(p=>p.ToShortDateString());
sushil pandey
  • 752
  • 10
  • 9