我正在使用WPF数据网格。我可以使用Linq从datagrid中检索行。可以Linq检索数据网格中的行
喜欢的东西:
List<People> people = from products in datagrid1 select products.ToList<People>();
我想没有吧?如果Linq支持数据网格,那将会很棒。
谢谢。
我正在使用WPF数据网格。我可以使用Linq从datagrid中检索行。可以Linq检索数据网格中的行
喜欢的东西:
List<People> people = from products in datagrid1 select products.ToList<People>();
我想没有吧?如果Linq支持数据网格,那将会很棒。
谢谢。
我怀疑你正在运行到的问题是,ItemsSource
是弱类型刚刚IEnumerable
- 而大多数的LINQ到对象适用于IEnumerable<T>
。您可以使用Cast<T>()
方法创建一个序列,以便在必要时投射每个项目。试试这个:
List<People> people = datagrid1.ItemsSource.Cast<People>().ToList();
注意,当你看到形式的阙RY表达from x in source select x
你应该使用source
只考虑 - 查询表达式不应盲目使用;找出每一个意思,然后确定它是否是表达你所需要的最恰当的方式。
如果你实际上想要一个更大的查询,你可能不想通过List<People>
。例如:
// Note the explicit typing of the range variable, which corresponds to a Cast
// call in the query translation.
var query = from People person in datagrid1.ItemsSource
where person.Age > 50
select person.Name;
(考虑的方式改变你的People
类型Person
,除非它确实代表了人的集合 - 在这种情况下,你应该给它它反映的是收集的真正含义的名称。 )
你应该能够查询数据网格的ItemSource
属性。
试试这个:
List<People> people = (from product in datagrid1.ItemsSource
select product).ToList<People>();
DataGrid1.DataSource属性中有什么? – Silagy
'.ToList()'明显不需要。 –
AgentFire
你为什么想要这样做?你可以绑定数据网格到一个集合,并直接查询集合... – doerig