用C#展平数组 [英] Flatten an Array in C#

查看:101
本文介绍了用C#展平数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,最简单的将数组展平的代码是什么?

In C# what is the shortest code to flatten an array?

例如,我想要

[[1,2],[2,3],[4,5]]

进入数组

[1,2,3,4,5]

我正在寻找最短的方法.

I am looking for the shortest way to do so.

推荐答案

也许我正在以错误的方式读取最短代码",但是我建议使用LINQ SelectManyDistinct:

Maybe I'm reading "shortest code" the wrong way, but I would propose using LINQ SelectMany and Distinct:

var values = new[]
{
    new[] { 1, 2 },
    new[] { 2, 3 },
    new[] { 4, 5 },
};

var flattenedUniqueValues = values.SelectMany(x => x).Distinct();

这篇关于用C#展平数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆