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吗? 如果需要怎么转化? 能否不转化?
{
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吗? 如果需要怎么转化? 能否不转化?
而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();