public string[] GetHeaders(string feature){
var result = dataGridView1.Columns
.OfType<DataGridViewColumn>()
.Where(c=>dataGridView1.Rows
.OfType<DataGridViewRow>()
.Any(r=>r.Cells["feature"].Value.ToString() == feature) &&
r.Cells[c.Name].Value.ToString() == "yes"))
.Select(c=>c.HeaderText)
.ToArray();
return result;
}
或这个:
public string[] GetHeaders(string feature){
var row = dataGridView1.Rows
.OfType<DataGridViewRow>()
.FirstOrDefault(r=>r.Cells["feature"].Value.Equals(feature));
if(row == null) return null;
return row.Cells.OfType<DataGridViewCell>()
.Where(c=>c.Value.Equals("yes"))
.Select(c=>c.OwningColumn.HeaderText)
.ToArray();
}
我认为后者更好。