简体   繁体   English

我可以在列表中使用匿名类型吗? <T> 而不是帮助类?

[英]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). 现在我发现为我的计算引入一个类型(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. 我的第一种方法是使用Tuple<DateTime, TimeSpan, bool>就像我通常这样做但因为我需要在创建实例后修改TimeSpan,所以我不能使用Tuple,因为Tuple.ItemX是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: 可以使用LINQ执行此操作:

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! 但到那时, Tuple更具可读性和简洁性!

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. Zip调用将每个日期与其后续日期配对,将每对值转换为持续时间和启用标志。 The Where call filters out disabled pairs. Where调用过滤掉禁用的对。 The Aggregate call sums the durations from the resulting pairs. Aggregate调用对结果对中的持续时间求和。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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