Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Linq从DataGridViewCellCollection中选择值_C#_Linq - Fatal编程技术网

C# 使用Linq从DataGridViewCellCollection中选择值

C# 使用Linq从DataGridViewCellCollection中选择值,c#,linq,C#,Linq,我有一个DataGridViewCellCollection,希望从单元格中读取值。稍后我想创建一个新对象,将这些值作为构造函数参数传入,并将它们添加到列表中 List<Connection> connections = new List<Connection>(); for (int i = 0; i < dataGridView.Rows.Count; i++) { DataGridVie

我有一个DataGridViewCellCollection,希望从单元格中读取值。稍后我想创建一个新对象,将这些值作为构造函数参数传入,并将它们添加到列表中

        List<Connection> connections = new List<Connection>();

        for (int i = 0; i < dataGridView.Rows.Count; i++)
        {
            DataGridViewCellCollection cells = dataGridView.Rows[i].Cells; // current cellrow
            int firstValue = (int)cells[1].Tag; // convert to int
            int? secondValue = (int?)cells[0].Value; // convert to nullable int
            connections.Add(new Connection(firstValue, secondValue));
        }

        return connections;
我想用Linq重写这段代码,但如何选择特定的多个值并将结果强制转换为一个对象

from row in datagridView.Rows
let cells = row.Cells
let firstValue = (int)cells[1].Tag
let secondValue = (int?)cells[0].Value
select new Connection(firstValue, secondValue)
正如@Mark Schultheiss所建议的,使用.Cast可以更快,如下所示

List<Connection> connections = dataGridView.Rows.Cast<DataGridViewRow>()
             .Select(x => new Connection((int)x.Cells[1].Tag, (int?)x.Cells[0].Value)
             .ToList();

return connections;

你能发布连接的类定义吗?当然,对不起,我更新了我的问题。对不起,行没有选择方法。我必须转换它吗?它应该是dataGridView.Rows.OfType,而不是dataGridView.Row或cast.CastI只需要将行转换为具有Select方法的内容,因为行没有。而且我无法将行转换为listWell,现在我可以添加注释了。确保您使用的是System.Linq:它应该是dataGridView.Rows.OfType。选择。。。而不是datagridView.Rows.Select..我设置了格式以便更容易查看,不确定OP是否希望使用.ToList实现它,但似乎是这样?@andy填充更多/所有配置文件以获得额外的几点:。由于我们可以假设该类型的行,所以强制转换可能会更快@感谢您的评论。我已经更新了我的答案。
var connections = dataGridView.Rows.OfType<DataGridViewRow>()
.Select(r => new Connection((int)r.Cells[1].Tag, (int?)r.Cells[0].Value))
List<Connection> connections = dataGridView.Rows.OfType<DataGridViewRow>()
             .Select(x => new Connection((int)x.Cells[1].Tag, (int?)x.Cells[0].Value)
             .ToList();

return connections;
List<Connection> connections = dataGridView.Rows.Cast<DataGridViewRow>()
             .Select(x => new Connection((int)x.Cells[1].Tag, (int?)x.Cells[0].Value)
             .ToList();

return connections;
List<Connection> connections = dataGridView.Rows.OfType<DataGridViewRow>()
                                           .Select(CreateConnection)
                                           .ToList();
var connections = from row in dataGridView.Rows.OfType<DataGridViewRow>()
                  select CreateConnection(row);
private Connection CreateConnection(DataGridViewRow row)
{
    var firstValue = (int) row.Cells[1].Tag;
    var secondValue = (int?) row.Cells[0].Value;

    return new Connection(firstValue, secondValue);
}