为每个键返回不同的列表[英] return different lists for each key

本文是小编为大家收集整理的关于为每个键返回不同的列表的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

i有一个表格中的表作为字符串列表[](每个字符串[]是列).

.

Column1| Column2  | Column3
--------+---------+----------
0       | 1       | 8
3       | 2       | 3
5       | 2       | 8

说:

string[] column1 = { 0, 3, 5 }
string[] column2 = { 1, 2, 2 };
string[] column3 = { 8, 3, 8 };
List<string[]> table = new List<string[]>() { column1, column2, column3 };

我想选择一个列(即列1)groupby column3,并在列3中创建一个带有每个不同值的列表.换句话说:组Column1 by column3并为列的每个不同值创建一个列.

输出将是:

string[] result1 = { 3 };  // From column3[1] = 3
string[] result2 = { 0, 5 };  // From column3[0] = column3[2] = 8

这是 post ,此一个,而简单1来自 msdn . 我考虑使用列1和第3列创建一个对象,然后按照此 post :

Class Row { public Row(string row1, string row3); }
List<Row> rows = new List<Row>();
for(int i = 0; i < column1.Length; i++)
{ rows.Add(new Row(Column1[i], Column3[i])); }

var output = rows.GroupBy(row => row.row3).ToDictionary(grp => grp.Key, grp => grp.ToList());

但是此代码有点丑陋.没有

之类的东西
column3.selectmany(...).GroupBy(row => row.row3).ToDictionary(grp => grp.Key, grp => grp.ToList());

我的意思是,某些表达式无需创建新类并填写一个对象列表...另外,我希望作为输出

string[] result1 = { 3 };  // From column3[1] = 3
string[] result2 = { 0, 5 };  // From column3[0] = column3[2] = 8

推荐答案

而不是创建新类型仅用于分组,您可以使用 Zip Zip Zip Zip 扩展方法和 anonymous type 创建行.

分组非常简单.每个组都有一个表示列3的键,IGrouping本身是一个IEnumerable,其中包含您仅选择列1的行:

.
var rows = column1.Zip(column3, (c1, c3) => new
{
    Column1 = c1,
    Column3 = c3
});

var output = from row in rows
             group row by row.Column3 into groupedRows
             select groupedRows.Select(r => r.Column1).ToArray();

这会产生IEnumerable<string[]>.

其他推荐答案

使用Zip的pescolino答案非常好.如果您不能使用它(例如,如果您不在.NET 4.X上),则可以使用Select的索引器置换来产生所需的结果:

var res = col1.Select((fld,idx) => new { Field = fld, Index = idx })
          .GroupBy(entry => col3[entry.Index], entry => entry.Field)
          .Select(grp => grp.ToArray());

本文地址:https://www.itbaoku.cn/post/1557192.html

问题描述

I have a table that is parsed in code as a List of string[] (each string[] is a column).

Column1| Column2  | Column3
--------+---------+----------
0       | 1       | 8
3       | 2       | 3
5       | 2       | 8

Let´s say:

string[] column1 = { 0, 3, 5 }
string[] column2 = { 1, 2, 2 };
string[] column3 = { 8, 3, 8 };
List<string[]> table = new List<string[]>() { column1, column2, column3 };

I want to select a column (i.e. Column1) groupby Column3, and create a list with each different value in Column3. In other words: group Column1 by column3 and create a Column for each different value of Column3.

The output would be:

string[] result1 = { 3 };  // From column3[1] = 3
string[] result2 = { 0, 5 };  // From column3[0] = column3[2] = 8

It´s a mix of this post, this one, and Simple 1 from msdn. I think about creating a object with column1 and column3, and then do as this post:

Class Row { public Row(string row1, string row3); }
List<Row> rows = new List<Row>();
for(int i = 0; i < column1.Length; i++)
{ rows.Add(new Row(Column1[i], Column3[i])); }

var output = rows.GroupBy(row => row.row3).ToDictionary(grp => grp.Key, grp => grp.ToList());

But this code code is a bit ugly. Isn´t there something like

column3.selectmany(...).GroupBy(row => row.row3).ToDictionary(grp => grp.Key, grp => grp.ToList());

I mean, some expression without the need of creating a new class and fill a list of objects... Also, I want as output

string[] result1 = { 3 };  // From column3[1] = 3
string[] result2 = { 0, 5 };  // From column3[0] = column3[2] = 8

推荐答案

Instead of creating a new type just for grouping you can use the Zip extension method and an anonymous type to create the rows.

Grouping is pretty straightforward then. Each group has a key which represents column3 and the IGrouping itself is an IEnumerable containing the rows from which you select only column 1:

var rows = column1.Zip(column3, (c1, c3) => new
{
    Column1 = c1,
    Column3 = c3
});

var output = from row in rows
             group row by row.Column3 into groupedRows
             select groupedRows.Select(r => r.Column1).ToArray();

This produces an IEnumerable<string[]>.

其他推荐答案

pescolino's answer using Zip is quite nice. If you can't use it (e.g. if you are not on .NET 4.x) then you could use the indexer-overload of Select to produce the desired result:

var res = col1.Select((fld,idx) => new { Field = fld, Index = idx })
          .GroupBy(entry => col3[entry.Index], entry => entry.Field)
          .Select(grp => grp.ToArray());