查询主体必须以select子句或group子句结束... Datagridview

时间:2017-05-04 19:24:36

标签: c# linq

引发错误

  

查询正文必须以select子句或group子句结尾

List<DataGridViewRow> selectedRows = 
    (from row in DataGridView1.Rows.Cast<DataGridViewRow>()
     where Convert.ToBoolean(row.Cells("CheckBoxColumn").Value) == true).ToList();

2 个答案:

答案 0 :(得分:1)

你应该改变这个

from row in DataGridView1.Rows.Cast<DataGridViewRow>()
where Convert.ToBoolean(row.Cells("CheckBoxColumn").Value) == true

到这个

from row in DataGridView1.Rows.Cast<DataGridViewRow>()
where Convert.ToBoolean(row.Cells("CheckBoxColumn").Value) == true
select row

如果您使用以下语法编写整个查询,那么您将获得相同的结果,这是非常了不起的:

var selectedRows = DataGridView1.Rows
                                .Cast<DataGridViewRow>()
                                .Where(row => Convert.ToBoolean(row.Cells("CheckBoxColumn").Value) == true)
                                .ToList();

答案 1 :(得分:1)

在查询语法中编写linq查询时,它必须有一个select clause

var result = from item in collection
             select item;

在你的情况下:

var result = (from row in DataGridView1.Rows.Cast<DataGridViewRow>()
              where Convert.ToBoolean(row.Cells("CheckBoxColumn").Value)
              select row).ToList();

请注意,执行Convert.ToBoolean(row.Cells("CheckBoxColumn").Value) == true是多余的,因为Convert.ToBoolean(row.Cells("CheckBoxColumn").Value)已经是布尔值