linq基础问题

论文MVP网 2009-12-12 11:29:40
private void button2_Click(object sender, EventArgs e)
{
string[] citys = { "juzi","mako","pingguo","aaa"};
IEnumerable<string> place = from city in citys where city.Length>2 select city;
this.dataGridView1.DataSource = dt;
}


就这个代码,同样的代码在web中可以显示, 但是在winform中无法显示, 难道winform中必须需要把他转化成datatable吗? 如果需要怎么转化? 能否不转化?
...全文
64 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
宝_爸 2009-12-12
  • 打赏
  • 举报
回复
dt从哪里来的?
qianshangsheqin 2009-12-12
  • 打赏
  • 举报
回复
你的dt是什么阿,datagridview的datasource是object类型的,不是非要转换成datatable.
别样苍茫 2009-12-12
  • 打赏
  • 举报
回复
winform中没用过,,,友情帮顶
wuyq11 2009-12-12
  • 打赏
  • 举报
回复
DataGridView的DataSource属性为object类型
而LINQ查询结果为IEnumerable<T>或IQueryable<T>类型,如果直接作为数据源绑定到DataGridView,将无法显示任何内容。Enumerable类为IEnumerable<T>接口定义了一系列扩展方法,其中的ToList<T>方法可以将IEnumerable<T>转换为List<T>。
var test = new List<string> { "a", "b", "c" };
dataGridView1.DataSource = (from s in test select new { s }).ToList();

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧