transforming to linq

本文关键字:linq to transforming | 更新日期: 2023-09-27 18:29:11

是否可以将以下代码转换为Linq,使其看起来更像

foreach (var entry in bottleneck.Item2)
{
    if(entry.ChangeoverTime > 0)
    {
        avarangechangeOverTimes += entry.ChangeoverTime;
        ++counter;
    }
    if (entry.ChangeoverTime > maxchangeOverTimes)
    {
        maxchangeOverTimes = entry.ChangeoverTime;
    }
    changeovertime.ChangeoverTimes.Add
    (
        new ChangeOverDateValue
        {
            ChangeoverValue = entry.ChangeoverTime,
            Color = ChangeOverTimeToColor(entry.ChangeoverTime),
            StartTime = entry.StartTime
        }
     );
}

transforming to linq

如果bottleneck.Item2中的条目数量不多,则可以使用以下三个语句实现相同的目的:

var maxChangeOverTime = bottleneck.Item2.Max(x => x.ChangeoverTime);
var averageChangeOverTime = bottleneck.Item2.Average(x => x.ChangeoverTime);
var result
    = bottleneck.Item2
                .Select(x => new ChangeOverDateValue
                             {
                                 ChangeoverValue = x.ChangeoverTime,
                                 Color = ChangeOverTimeToColor(x.ChangeoverTime),
                                 StartTime = x.StartTime
                             });
changeovertime.ChangeoverTimes.AddRange(result);

请注意,这将使用当前代码枚举bottleneck.Item2三次,而不是一次。

我认为您只能更改外部foreach:

bottleneck.Items2.ToList().Foreach(entry =>
{
// your code
});
bottleneck.Item2.where(entry => entry.ChangeoverTime).ToList().Foreach(entry => {
    avarangechangeOverTimes += entry.ChangeoverTime;
    ++counter;
});

maxchangeOverTimes = bottleneck.Item2.max=(entry => entry.ChangeoverTime);
changeovertime.ChangeoverTimes.AddAll(bottleneck.Item2.select(entry => new new ChangeOverDateValue
                    {
                        ChangeoverValue = entry.ChangeoverTime,
                        Color = ChangeOverTimeToColor(entry.ChangeoverTime),
                        StartTime = entry.StartTime
                    }));

这就是我在没有更多信息或我的开发环境的情况下所能想到的

也许。。。

var avarangechangeOverTimes = bottleneck.Item2
                                    .Where(entry => entry.ChangeOverTime > 0)
                                    .Sum(entry => entry.ChangeOverTime);
var maxchangeOverTimes = bottleneck.Item2
                                    .Max(entry => entry.ChangeOverTime);
bottleneck.Item2.ToList()
        .ForEach(entry => changeovertime.ChangeoverTimes.Add
                                    (
                                        new ChangeOverDateValue {
                                            ChangeoverValue = entry.ChangeoverTime,
                                            Color = ChangeOverTimeToColor(entry.ChangeoverTime),
                                            StartTime = entry.StartTime
                                        }
                                   )
                                   );

试试这个:

var averagechangeOverTimes = bottleneck.Item2
                                       .Where(entry => entry.ChangeoverTime > 0)
                                       .Sum(entry => entry.ChangeoverTime);
counter = bottleneck.Item2.Count(entry => entry.ChangeoverTime > 0);
var maxchangeOverTimes = bottleneck.Item2.Max(entry => entry.ChangeoverTimes);
changeovertime.ChangeoverTime.Add(bottleneck.Item2
                                            .Select(entry => new ChangeOverDateValue{
                                                        ChangeoverValue = entry.ChangeoverTime,
                                                        Color = ChangeOverTimeToColor(entry.ChangeoverTime),
                                                        StartTime = entry.StartTime
                                                      });