如何在C#LINQ中迭代列表

时间:2016-05-10 00:57:31

标签: c# linq

上述三角形的累积版本为:

enter image description here

示例输入数据:短输入文件可能包含以下内容:

import multiprocessing as mp
from datetime import datetime, timedelta
from joblib import Parallel, delayed
from joblib.pool import has_shareable_memory
from periphery_model import CochleaModel

class Periphery: 
    def init():
        self.cochlear_list= [[CochleaModel(), self.stimulus[i], self.irr_on[i], i, (0, i + 1)] for i in range(len(self.stimulus))]

    def run(self) -> [PeripheryOutput]:
            """Simulate sound propagation up to the auditory nerve for many stimulus levels
            :return: A list of output data, one for each stimulus level
            """
            s1 = datetime.now()
            results = Parallel(n_jobs=mp.cpu_count(), max_nbytes=10e6)(delayed(self.solve_one_cochlea, has_shareable_memory)(xx) for xx in self.cochlear_list)

            self.save_model_configuration()
            print("\ncochlear simulation of {} stimulus levels finished in {:0.3f}s".format(len(self.stimulus),                                                                                                timedelta.total_seconds(
                datetime.now() - s1)))
            return results

此示例文件包含两个三角形 - 一个用于名为“Comp”的产品,另一个用于名为“Non-Comp”的产品。第一行包含列标题,后续行包含数据,因此,例如,1990年非Comp产品发生的事故,1990年支付了45.2,1991年支付了64.8,1993年支付了37。

与上述输入文件对应的输出文件为:

1990, 4 
Comp, 0, 0, 0, 0, 0, 0, 0, 110, 280, 200 
Non-Comp, 45.2, 110, 110, 147, 50, 125, 150, 55, 140, 100 

第一行给出了最早的起源年份(即1990年)和发展年份(在这种情况下从1990年到1993年,即4)。

在第一行之后,每个三角形都有一条线。该行中的第一个字段给出了产品的名称。后续字段是累积的三角形值。

到目前为止,我已经建立了两个类来实现这一目标。

var products = new List<Products>() { new Products { ProductName = "Comp", OriginYear = 1992, DevelopmentYear = 1992, IncrementalValue = 110.0 }, new Products { ProductName = "Comp", OriginYear = 1992, DevelopmentYear = 1993, IncrementalValue = 170.0 }, new Products { ProductName = "Comp", OriginYear = 1993, DevelopmentYear = 1993, IncrementalValue = 200.0 }, new Products { ProductName = "Non-Comp", OriginYear = 1990, DevelopmentYear = 1990, IncrementalValue = 45.2 }, new Products { ProductName = "Non-Comp", OriginYear = 1990, DevelopmentYear = 1991, IncrementalValue = 64.8 }, new Products { ProductName = "Non-Comp", OriginYear = 1990, DevelopmentYear = 1993, IncrementalValue = 37.0 }, new Products { ProductName = "Non-Comp", OriginYear = 1991, DevelopmentYear = 1991, IncrementalValue = 50.0 }, new Products { ProductName = "Non-Comp", OriginYear = 1991, DevelopmentYear = 1992, IncrementalValue = 75.0 }, new Products { ProductName = "Non-Comp", OriginYear = 1991, DevelopmentYear = 1993, IncrementalValue = 25.0 }, new Products { ProductName = "Non-Comp", OriginYear = 1992, DevelopmentYear = 1992, IncrementalValue = 55.0 }, new Products { ProductName = "Non-Comp", OriginYear = 1992, DevelopmentYear = 1993, IncrementalValue = 85.0 }, new Products { ProductName = "Non-Comp", OriginYear = 1993, DevelopmentYear = 1993, IncrementalValue = 100.0 }, };

Products

public class Products : INotifyPropertyChanged { string _ProductName; int _OriginYear; int _DevelopmentYear; double _IncrementalValue; public string ProductName { get { return _ProductName; } set { if (_ProductName != value) { _ProductName = value; RaisePropertyChanged("ProductName"); } } } public int OriginYear { get { return _OriginYear; } set { if (_OriginYear != value) { _OriginYear = value; RaisePropertyChanged("OriginYear"); } } } public int DevelopmentYear { get { return _DevelopmentYear; } set { if (_DevelopmentYear != value) { _DevelopmentYear = value; RaisePropertyChanged("DevelopmentYear"); } } } public double IncrementalValue { get { return _IncrementalValue; } set { if (_IncrementalValue != value) { _IncrementalValue = value; RaisePropertyChanged("IncrementalValue"); } } } void RaisePropertyChanged(string prop) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); } } public event PropertyChangedEventHandler PropertyChanged; }

ProductViewModel

1 个答案:

答案 0 :(得分:0)

如果您想要那样的输出,您可以链接一些Linq,如下所示。

IEnumerable<string> result = products
   .GroupBy(x => x.ProductName)
   .Select(product 
        => product.Key
           + new string(product
                       .SelectMany(x 
                                => ", " + x.IncrementalValue.ToString(CultureInfo.CurrentCulture))
                                    .ToArray()));

从这里,您可以遍历结果以获得所需的输出。