2012-03-28 104 views
0

是否有可能下面的代碼轉換到LINQ,它看起來像多一點很好轉換到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 
     } 
    ); 
} 

回答

3

如果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三次而不是一次與您當前的代碼。

+0

Depends中在什麼瓶頸上。它將始終迭代,但如果瓶頸是dbcontext,那麼linq語句將被轉換爲sql。除此之外,那+1最好的解決方案^^ – Frederiek 2012-03-28 11:53:11

0

我認爲你可以只改變外部的foreach:

bottleneck.Items2.ToList().Foreach(entry => 
{ 
// your code 
}); 
0
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 
        })); 

這是所有我可以拿出無需更多信息或我與我的發展envirment的

0

也許......

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 
             } 

            ) 
            ); 
0

試試這個:

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 
                 });