简体   繁体   中英

Can I use an anonymous type in a List<T> instead of a helper class?

I need a list with some objects for calculation.

my current code looks like this

private class HelperClass
{
    public DateTime TheDate {get;set;}
    public TimeSpan TheDuration {get;set;}
    public bool Enabled {get;set;}
}

private TimeSpan TheMethod()
{
    // create entries for every date
    var items = new List<HelperClass>();
    foreach(DateTime d in GetAllDatesOrdered())
    {
        items.Add(new HelperClass { TheDate = d, Enabled = GetEnabled(d), });
    }

    // calculate the duration for every entry
    for (int i = 0; i < items.Count; i++)
    {
        var item = items[i];
        if (i == items.Count -1) // the last one
            item.TheDuration = DateTime.Now - item.TheDate;
        else
            item.TheDuration = items[i+1].TheDate - item.TheDate;
    }

    // calculate the total duration and return the result
    var result = TimeSpan.Zero;
    foreach(var item in items.Where(x => x.Enabled))
        result = result.Add(item.TheDuration);
    return result;
}

Now I find it a bit ugly just to introduce a type for my calculation (HelperClass). My first approach was to use Tuple<DateTime, TimeSpan, bool> like I usually do this but since I need to modify the TimeSpan after creating the instance I can't use Tuple since Tuple.ItemX is readonly.

I thought about an anonymous type, but I can't figure out how to init my List

var item1 = new { TheDate = DateTime.Now,
                  TheDuration = TimeSpan.Zero, Enabled = true };

var items = new List<?>(); // How to declare this ???
items.Add(item1);

You could do it with LINQ like:

var itemsWithoutDuration = GetAllDatesOrdered()
    .Select(d => new { TheDate = d, Enabled = GetEnabled(d) })
    .ToList();
var items = itemsWithoutDuration
    .Select((it, k) => new { TheDate = it.d, Enabled = it.Enabled, 
         TheDuration = (k == (itemsWithoutDuration.Count - 1) ? DateTime.Now : itemsWithoutDuration[k+1].TheDate) - it.TheDate })
    .ToList();

But by that point the Tuple is both more readable and more concise!

Using a projection looks like the way forward to me - but you can compute the durations as you go, by "zipping" your collection with itself, offset by one. You can then do the whole method in one query:

// Materialize the result to avoid computing possibly different sequences
var allDatesAndNow = GetDatesOrdered().Concat(new[] { DateTime.Now })
                                      .ToList();

return allDatesNow.Zip(allDatesNow.Skip(1),
                       (x, y) => new { Enabled = GetEnabled(x),
                                       Duration = y - x })
                  .Where(x => x.Enabled)
                  .Aggregate(TimeSpan.Zero, (t, pair) => t + pair.Duration);

The Zip call pairs up each date with its subsequent one, converting each pair of values into a duration and an enabled flag. The Where call filters out disabled pairs. The Aggregate call sums the durations from the resulting pairs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM