Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C Linq通过键组合多个序列_C#_Linq_Linq To Objects - Fatal编程技术网

C# C Linq通过键组合多个序列

C# C Linq通过键组合多个序列,c#,linq,linq-to-objects,C#,Linq,Linq To Objects,我需要合并两个IList 流量是一个简单的类: class Traffic { long MegaBits; DateTime Time; } 每个IList都包含相同的时间,我需要一个IList,在这里我总结了兆位,但保留了时间作为键 这可以使用Linq吗 编辑: 我忘了提到时间在任何列表中都不一定是唯一的,多个流量实例可能有相同的时间 此外,我可能会遇到2个以上的X列表,我也应该提到这一点-抱歉:- 例如: IEnumerable<IList<Traffic>&g

我需要合并两个IList

流量是一个简单的类:

class Traffic
{
  long MegaBits;
  DateTime Time;
}
每个IList都包含相同的时间,我需要一个IList,在这里我总结了兆位,但保留了时间作为键

这可以使用Linq吗

编辑:

我忘了提到时间在任何列表中都不一定是唯一的,多个流量实例可能有相同的时间

此外,我可能会遇到2个以上的X列表,我也应该提到这一点-抱歉:-

例如:

IEnumerable<IList<Traffic>> trafficFromDifferentNics;

var combinedTraffic = trafficFromDifferentNics
                        .SelectMany(list => list)
                        .GroupBy(traffic => traffic.Time)
                        .Select(grp => new Traffic { Time = grp.Key, MegaBits = grp.Sum(tmp => tmp.MegaBits) });
上面的示例很有效,因此感谢您的输入:-

听起来像:

var query = from x in firstList
            join y in secondList on x.Time equals y.Time
            select new Traffic { MegaBits = x.MegaBits + y.MegaBits,
                                 Time = x.Time };
请注意,这将以成对方式连接,因此如果每个列表中有多个元素同时存在,则可能无法获得所需的结果。

听起来像:

var query = from x in firstList
            join y in secondList on x.Time equals y.Time
            select new Traffic { MegaBits = x.MegaBits + y.MegaBits,
                                 Time = x.Time };
请注意,这将以成对方式连接,因此如果每个列表中有多个元素同时存在,则可能无法获得所需的结果。

这听起来更像

var store = firstList.Concat(secondList).Concat(thirdList)/* ... */;
var query = from item in store
            group item by item.Time
            into groupedItems
            select new Traffic
            {
                MegaBits = groupedItems.Sum(groupedItem => groupedItem.MegaBits),
                Time = groupedItems.Key
            };
或者,用你的返工

IEnumerable<IList<Traffic>> stores;
var query = from store in stores
            from item in store
            group item by item.Time
            into groupedItems
            select new Traffic
            {
                MegaBits = groupedItems.Sum(groupedItem => groupedItem.MegaBits),
                Time = groupedItems.Key
            };
这听起来更像

var store = firstList.Concat(secondList).Concat(thirdList)/* ... */;
var query = from item in store
            group item by item.Time
            into groupedItems
            select new Traffic
            {
                MegaBits = groupedItems.Sum(groupedItem => groupedItem.MegaBits),
                Time = groupedItems.Key
            };
或者,用你的返工

IEnumerable<IList<Traffic>> stores;
var query = from store in stores
            from item in store
            group item by item.Time
            into groupedItems
            select new Traffic
            {
                MegaBits = groupedItems.Sum(groupedItem => groupedItem.MegaBits),
                Time = groupedItems.Key
            };

您可以将两个列表中的项目组合成一个集合,然后在将其转换回一组新的流量实例之前,对键进行分组以获得总和

var result = firstList.Concat(secondList)
    .GroupBy(trf => trf.Time, trf => trf.MegaBits)
    .Select(grp => new Traffic { Time = grp.Key, MegaBits = grp.Sum()});

您可以将两个列表中的项目组合成一个集合,然后在将其转换回一组新的流量实例之前,对键进行分组以获得总和

var result = firstList.Concat(secondList)
    .GroupBy(trf => trf.Time, trf => trf.MegaBits)
    .Select(grp => new Traffic { Time = grp.Key, MegaBits = grp.Sum()});

嗯,我想我应该这样写——时间在任何列表中都不一定是唯一的。为了清楚起见,我会更新这个问题。@Steffen:在这一点上,如果你能给出一个输入和预期输出的样本,那将非常有帮助。嗯,我想我应该这样写-时间不保证在任何列表中都是唯一的。为了清楚起见,我会更新这个问题。@Steffen:在这一点上,如果你能给出一个输入和预期输出的样本,那将非常有帮助。