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”.

效果图如下:

             WPF 绑定各种数据源之LINQ检索结果

 2、如果数据源已经是DataTable,则使用LINQ检索结果的形式为请参考: WPF 绑定各种数据源之Datatable 第三点(3、如果数据源已经是DataTable,则使用LINQ检索结果的形式为)。

 

 

 

 

 

 

 

 

相关文章:

  • 2021-10-31
  • 2022-12-23
  • 2021-10-01
  • 2021-08-26
  • 2021-09-10
  • 2021-06-15
  • 2022-12-23
猜你喜欢
  • 2021-05-09
  • 2021-06-05
  • 2021-10-18
相关资源
相似解决方案