在列表中连接列表而无需循环的最有效方法

发布于 2024-12-06 18:31:38 字数 398 浏览 0 评论 0原文

public class Unicorn
{
    public List<int> Numbers { get; set; }
}

unicorns.Add(new Unicorn() { Numbers = {1, 2, 3} } );
unicorns.Add(new Unicorn() { Numbers = {4, 5, 6} } );
unicorns.Add(new Unicorn() { Numbers = {7, 8, 9} } );

c# 4 中将所有列表连接成一个 {1, 2, 3, 4, 5, 6, 7, 8, 9 } 列表的最有效方法是什么?

最好(理想情况下;优先考虑;如果有选择的话)没有循环并且无 Linq。我对 .FindAll 进行了修改,但它没有挖掘它。

public class Unicorn
{
    public List<int> Numbers { get; set; }
}

unicorns.Add(new Unicorn() { Numbers = {1, 2, 3} } );
unicorns.Add(new Unicorn() { Numbers = {4, 5, 6} } );
unicorns.Add(new Unicorn() { Numbers = {7, 8, 9} } );

What's the most efficient way in c# 4 to concatenate all the lists into one list of {1, 2, 3, 4, 5, 6, 7, 8, 9 } ?

Preferably (ideally; by preference; if one had a choice) no loops and Linq-less. I tinkered around with .FindAll, but it's not digging it.

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

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

发布评论

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

评论(4

冷清清 2024-12-13 18:31:38
List<int> theInts = unicorns.SelectMany(unicorn => unicorn.Numbers).ToList();

如果没有测量,从性能角度来看,这里唯一让我犹豫的是 .ToList

如果有大量数字,ToList 可能会重复地重新分配其支持数组。为了避免这种行为,您必须了解预期有多少个数字。

List<int> theInts = new List<int>(expectedSize);
theInts.AddRange(unicorns.SelectMany(unicorn => unicorn.Numbers));
List<int> theInts = unicorns.SelectMany(unicorn => unicorn.Numbers).ToList();

Without measuring, the only thing in here that gives me pause from a performance perspective is the .ToList

If there are a TON of numbers, it's possible that ToList may repeatedly re-allocate its backing array. In order to escape this behavior, you have to have some idea about how many Numbers to expect.

List<int> theInts = new List<int>(expectedSize);
theInts.AddRange(unicorns.SelectMany(unicorn => unicorn.Numbers));
狼性发作 2024-12-13 18:31:38

这是一个完全满足您要求的解决方案: Linq 和循环 - 我很确定您不想使用此代码:

List<Unicorn> unicorns = new List<Unicorn>();
unicorns.Add(new Unicorn() { Numbers = new List<int>{1, 2, 3} } );
unicorns.Add(new Unicorn() { Numbers = new List<int> { 4, 5, 6 } });
unicorns.Add(new Unicorn() { Numbers = new List<int> { 7, 8, 9 } });

List<int> numbers = new List<int>();
int count = 0;

AddUnicorn: 
if(count < unicorns.Count)
{
    numbers.AddRange(unicorns[count++].Numbers);
    goto AddUnicorn;
}

Here's a solution that fully meets your requirements: No Linq and no loop - I'm pretty sure you do not want to use this code though:

List<Unicorn> unicorns = new List<Unicorn>();
unicorns.Add(new Unicorn() { Numbers = new List<int>{1, 2, 3} } );
unicorns.Add(new Unicorn() { Numbers = new List<int> { 4, 5, 6 } });
unicorns.Add(new Unicorn() { Numbers = new List<int> { 7, 8, 9 } });

List<int> numbers = new List<int>();
int count = 0;

AddUnicorn: 
if(count < unicorns.Count)
{
    numbers.AddRange(unicorns[count++].Numbers);
    goto AddUnicorn;
}
初见终念 2024-12-13 18:31:38

你的要求很不寻常,但我想你总是可以写:

var numbers = new List<int>();
unicorns.ForEach(unicorn => numbers.AddRange(unicorn.Numbers));

ForEach( ) 可以说符合“LINQ-less”,因为它是 List 的真正成员,而不是IEnumerable,它实际上早于 LINQ 本身。

Your requirements are quite out of the ordinary, but I guess you can always write:

var numbers = new List<int>();
unicorns.ForEach(unicorn => numbers.AddRange(unicorn.Numbers));

ForEach() arguably qualifies as "LINQ-less", since it's a genuine member of List<T>, not an extension method on IEnumerable<T>, and it actually predates LINQ itself.

梦与时光遇 2024-12-13 18:31:38

使用 .NET 4.0 的 Zip 运算符:

     var sums = b.Zip(a, (x, y) => x + y)
        .Concat(b.Skip(a.Count()));

如果您想概括这一点,请检查哪个具有更多元素并将其用作上面的“b”

Using .NET 4.0's Zip operator:

     var sums = b.Zip(a, (x, y) => x + y)
        .Concat(b.Skip(a.Count()));

If you want to generalize this, check which has more elements and use that as the "b" above

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