使用 LINQ 根据记录 ID 获取最大 DataGridViewRow

ehh*_*ehh 3 c# linq datagridview winforms datagridviewrow

我正在尝试使用最大记录 ID 值获取 DataGridViewRow。我能够获得记录 ID 本身,但我需要找到该行本身。

这是我获取最大记录 ID 的代码,它工作正常:

var addedRow =
                dataGridView.Rows
                .Cast<DataGridViewRow>()
                .Where(r => r.Cells[Glossary.RowType].Value.ToString().Equals(Glossary.RowTypeCustom))
                .Select(r => Convert.ToInt(r.Cells[Glossary.RecordId].Value))
                .Max();
Run Code Online (Sandbox Code Playgroud)

我试过:

.Max(r => r.Cells[Glossary.RecordId].Value);
Run Code Online (Sandbox Code Playgroud)

我不清楚如何获取行本身,所以我可以关注它:

dataGridView.CurrentCell = addedRow;
Run Code Online (Sandbox Code Playgroud)

Vip*_*ock 5

尝试使用这个。

    var addedRow =
            dataGridView.Rows
            .Cast<DataGridViewRow>()
            .Where(r => r.Cells[Glossary.RowType].Value.ToString().Equals(Glossary.RowTypeCustom))
            .OrderByDescending(r => Convert.ToInt(r.Cells[Glossary.RecordId].Value))
            .FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)

希望它会有所帮助。

  • MAX 返回与字段相同类型的变量。因此,对于“Max”,您必须获取最大值,然后通过编写另一个 linq 来获取行的值。 (3认同)