LINQ查询的结果是一个IEnumberable<T>类型对象,而IEnumberable<T>类型对象有派生自IEnumberable,所以它可以作为列表控件的ItemsSource来使用.
我创建了一个Sutdent类:
public
class
Student
{
public
int
ID{
get
;
set
;}
public
string
Name {
get
;
set
; }
public
string
Age {
get
;
set
; }
}
|
UI的设计如下:
<ListView Background=
"LightCoral"
HorizontalAlignment=
"Left"
Margin=
"37,169,0,0"
Name=
"listView1"
Width=
"198"
Height=
"100"
VerticalAlignment=
"Top"
>
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding=
"{Binding ID}"
Header=
"ID"
/>
<GridViewColumn DisplayMemberBinding=
"{Binding Name}"
Header=
"Name"
/>
<GridViewColumn DisplayMemberBinding=
"{Binding Age}"
Header=
"Age"
/>
</GridView>
</ListView.View>
</ListView>
|
c#代码如下:
List<Student> studentList =
new
List<Student>()
{
new
Student(){ID=10,Name=
"Cat"
, Age=
"39"
},
new
Student(){ID=10,Name=
"Cat1"
, Age=
"99"
},
new
Student(){ID=10,Name=
"Dog"
, Age=
"89"
},
new
Student(){ID=10,Name=
"Dog1"
, Age=
"29"
},
};
listView1.ItemsSource =
from
stu
in
studentList
where
stu.Name.StartsWith(
"C"
)
select
stu;
|
显示的结果是Name以"C"开头的数据,分别为"Cat"和"Cat1”.
效果图如下:
2、如果数据源已经是DataTable,则使用LINQ检索结果的形式为请参考: WPF 绑定各种数据源之Datatable 第三点(3、如果数据源已经是DataTable,则使用LINQ检索结果的形式为)。
本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/archive/2011/09/17/2179698.html,如需转载请自行联系原作者