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/
欢迎任何形式的转载,未经作者同意,请保留此段声明!
浙公网安备 33010602011771号