我想包含来自该DataGridView中所有的 '选择' 系列代码的List<string>
:转换一个的IEnumerable <AnonymousType#1>列出<string>
我可以让他们作为一个DataGridViewRow的,如:
List<DataGridViewRow> selectedSeriesCodes =
gridSeriesList.Rows.Cast<DataGridViewRow>().Where(g =>
!string.IsNullOrEmpty(g.Cells[ListDataSource.gridColumn1].Value.ToString()) &&
Convert.ToBoolean(g.Cells[ListDataSource.gridColumn1].Value) == true).ToList();
但我的目标,让他们作为一个纯粹List<string>
为关注点分离,我不想控制传递到业务逻辑,以减轻测试,只是一个供参考,这是一个excel VSTO插件-在。
我想使用LINQ与匿名类型会做的伎俩,但它不会编译,如:
List<string> selectedSeriesCodes = from x in gridSeriesList.Rows.Cast<DataGridViewRow>()
where (!string.IsNullOrEmpty(x.Cells[ListDataSource.gridColumn1].Value.ToString()) &&
Convert.ToBoolean(x.Cells[ListDataSource.gridColumn1].Value) == true)
select new { SeriesCode = x.Cells[ListDataSource.gridColumn2].ToString() };
错误 'System.Collections.Generic.IEnumerable
<AnonymousType#1>
' 到 “System.Collections中.Generic.List<string>
'。一个显式转换 存在(是否缺少强制转换?)
我相信这是可能的,否则我就用一个for循环。任何指针非常感谢!
你并不需要选择一个新的匿名类型。只需'选择value.ToString();'并删除需要将结果转换为列表具体而言,请将业务代码需要IEnumerable 。 –
2012-03-13 03:40:25