我有一组选定的单元格。由于有多个列,集合中单元格的行索引通常采用 2、2、2、3、3、3、4、4、4 等格式。
我想获得这些单元格的行索引列表。
List<int> selectedRows = new List<int>();
List<DataGridViewCell> cellCollection = dGV_model.SelectedCells.Cast<DataGridViewCell>()
.GroupBy(cell => cell.RowIndex)
.Select(cell => cell.First())
.ToList<DataGridViewCell>();
foreach (DataGridViewCell cell in cellCollection)
{
selectedRows.Add(cell.RowIndex);
}
本质上,我的问题是如何从单个 LINQ 查询创建一个 int 列表?现在,我必须遍历 cellcollection 以将它们添加到 int 列表中。