C# Linq:选择 List<string> 中的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19138925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:12:57  来源:igfitidea点击:

Linq: select where in List<string>

c#asp.net-mvclinqlinq-to-excel

提问by stefjnl

Struggeling with some LinqToExcel filtering here...

在这里挣扎一些 LinqToExcel 过滤......

Ive got a List<string> columnsToFilterthat contains 9 strings, and with that i want to filter out the data for certain columns in a List<Row>, where Rowcontains the properties

我有一个List<string> columnsToFilter包含 9 个字符串的,并且我想过滤掉 a 中某些列的数据List<Row>,其中Row包含属性

IEnumerable<string> ColumnNames
Cell this[string columnName]

So: List<Row>has say 30 rows, each having 12 ColumnNames. Now i want to filter that List<Row>using List<string> columnsToFilterso that i end up with a List<Row>of 30 rows and 9ColumnNames.

所以:List<Row>有 30 行,每行有 12 个列名。现在我想筛选List<Row>使用List<string> columnsToFilter,使我有最后List<Row>的30行,9个COLUMNNAMES。

I can select the data for onecolumn by quering the columnname:

我可以通过查询列名来选择列的数据:

var result = content.Select(m => m["Column1"]).ToList();

Now i want to filter the data based on a Listof strings List<string> columnsToFilter. Whats the best way to achieve that?

现在我想根据字符串列表过滤数据List<string> columnsToFilter。实现这一目标的最佳方法是什么?

采纳答案by stefjnl

I ended up doing this in two steps:

我最终分两步完成:

        foreach (var column in columnNumbers)
        {
            yield return data.Select(m => m[column].Value.ToString()).ToList();
        }

Now I have the data I need, but with the rows and columns swapped, so i had to swap rows for columns and vice versa:

现在我有了我需要的数据,但是行和列交换了,所以我不得不交换列的行,反之亦然:

        for (int i = 1; i < rowCount; i++)
        {
            var newRow = new List<string>();

            foreach (var cell in list)
            {
                newRow.Add(cell[i]);
            }

            yield return newRow;
        }

回答by Dweeberly

Is this what you are looking for?

这是你想要的?

var colnames = new List<string>();
var rows = new Dictionary<string, object>();
var result = rows.Where(kv => colnames.Contains(kv.Key)).Select(kv => kv.Value);

回答by Robert Tanenbaum

Define an object called MyObject which has the property names corresponding to the 9 columns that you want to select.

定义一个名为 MyObject 的对象,该对象具有与要选择的 9 列对应的属性名称。

var excel = new ExcelQueryFactory("excelFileName");
var myObjects = from c in excel.Worksheet<MyObject>()
                       select c;

Bingo. You can now iterate through the 30 objects with the 9 columns as properties. Remember that LinqToExcel will happily fill objects even if they don't have all the columns represented.

答对了。您现在可以使用 9 列作为属性遍历 30 个对象。请记住,即使对象没有表示所有列,LinqToExcel 也会很高兴地填充对象。

You could even have a property or method as part of MyObject called "row" that would be a Dictionary() object so you could say myObject.row["ColumnName"] to reference a value if you preferred that syntax to just saying myObject.ColumnName to get the value. Personally I would rather deal with actual properties than to use the dictionary convolution.

你甚至可以有一个属性或方法作为 MyObject 的一部分,称为“row”,它是一个 Dictionary() 对象,所以你可以说 myObject.row["ColumnName"] 来引用一个值,如果你更喜欢这种语法而不是仅仅说 myObject。 ColumnName 来获取值。我个人宁愿处理实际属性而不是使用字典卷积。