C# Linq 按键组合多个序列

发布于 2024-10-20 06:46:20 字数 828 浏览 2 评论 0原文

我有两个需要合并的 IList

Traffic 是一个简单的类:

class Traffic
{
  long MegaBits;
  DateTime Time;
}

每个 IList 拥有相同的时间,我需要一个 IList,我在其中总结了兆位,但将时间保留为钥匙。

使用 Linq 可以吗?

编辑:

我忘了提及,时间在任何列表中不一定是唯一的,多个 Traffic 实例可能具有相同的时间。

另外,我可能会遇到 X 个列表(超过 2 个),我也应该提到这一点 - 抱歉 :-(

示例:

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

上面的示例有效,所以感谢您的输入 :-)

I have two IList<Traffic> I need to combine.

Traffic is a simple class:

class Traffic
{
  long MegaBits;
  DateTime Time;
}

Each IList holds the same Times, and I need a single IList<Traffic>, where I have summed up the MegaBits, but kept the Time as key.

Is this possible using Linq ?

EDIT:

I forgot to mention that Time isn't necessarily unique in any list, multiple Traffic instances may have the same Time.

Also I might run into X lists (more than 2), I should had mentioned that as well - sorry :-(

EXAMPLE:

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

The example above works, so thanks for your inputs :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一桥轻雨一伞开 2024-10-27 06:46:20

这听起来更像是

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

this sounds more like

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

or, with your rework

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
            };
晨光如昨 2024-10-27 06:46:20

您可以将两个列表中的项目组合成一个集合,然后对键进行分组以获得总和,然后再转换回一组新的 Traffic 实例。

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

You could combine the items in both lists into a single set, then group on the key to get the sum before transforming back into a new set of Traffic instances.

var result = firstList.Concat(secondList)
    .GroupBy(trf => trf.Time, trf => trf.MegaBits)
    .Select(grp => new Traffic { Time = grp.Key, MegaBits = grp.Sum()});
夏末 2024-10-27 06:46:20

这听起来像:

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

请注意,这将以成对的方式加入,因此如果每个列表中有多个具有相同时间的元素,您可能无法获得您想要的结果。

That sounds like:

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

Note that this will join in a pair-wise fashion, so if there are multiple elements with the same time in each list, you may not get the results you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文