c#数据绑定之linq使用示例

时间:2022-07-17 11:07:22 

c#数据绑定之linq使用示例

XAML:


<Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="2*"/>
                <ColumnDefinition Width="8*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
                <RowDefinition  />
            </Grid.RowDefinitions>
            <ListBox Grid.Column="1"  Margin="10" Name="listBox1"  />
            <Button Content="LinqToEntity" Grid.Column="0" Margin="5" Name="button1" Click="button1_Click" />
            <ListBox Grid.Column="1" Grid.Row="1"  Margin="10" Name="listBox2"  />
            <Button Content="LinqToSQL" Grid.Column="0" Grid.Row="1" Margin="5" Name="button2" Click="button2_Click" />
            <ListBox Grid.Column="1" Grid.Row="2"  Margin="10" Name="listBox3"  />
            <Button Content="LinToEnClient" Grid.Column="0" Grid.Row="2" Margin="5" Name="button3" Click="button3_Click"  />
        </Grid>

Button1 LINQ TO ENTITY


using (var context = new AdventureWorks2008Entities())
            {
                //var people = context.People.Where(c => c.LastName == "King").OrderBy(d => d.FirstName).Select(r => new { r.FirstName,r.LastName});
                //var people = context.People.Where(c => c.LastName == "King").OrderBy(c =>c.FirstName).Select(c => new { c.FirstName, c.LastName });
                var people = from per in context.People
                             //join emp in context.Employees on per.BusinessEntityID equals emp.BusinessEntityID
                             where per.LastName == "King"
                             orderby per.FirstName
                             select new { per.FirstName, per.LastName};
                foreach (var person in people)
                {
                    listBox1.Items.Add(string.Format("{0} \t \t {1} ", person.FirstName, person.LastName));
                }
            }

Button2 LINQ TO ENTITYSQL


using (var context = new AdventureWorks2008Entities())
            {
                var str = "SELECT VALUE p FROM AdventureWorks2008Entities.People AS p WHERE p.LastName= @LastName Order by p.FirstName";
                //var people = context.CreateQuery<Person>(str);
                var people = new System.Data.Objects.ObjectQuery<Person>(str, context);
                people.Parameters.Add(new System.Data.Objects.ObjectParameter("LastName", "King"));
                foreach (var person in people)
                {
                    listBox2.Items.Add(string.Format("{0} \t \t{1}", person.FirstName, person.LastName));
                }
            }

Button3 LINQ TO ENTITYCLIENT


var firstName = "";
            var lastName = "";
            using (EntityConnection conn = new EntityConnection("name=AdventureWorks2008Entities"))
            {
                string str = "SELECT p.FirstName, p.LastName FROM AdventureWorks2008Entities.People AS p WHERE p.LastName='King' Order by p.FirstName";
                conn.Open();
                EntityCommand cmd = conn.CreateCommand();
                cmd.CommandText =str;
                using (EntityDataReader rdr = cmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess))
                {
                    while (rdr.Read())
                    {
                        firstName = rdr.GetString(0);
                        lastName = rdr.GetString(1);
                        listBox3.Items.Add(string.Format("{0}\t \t{1}", firstName, lastName));
                    }
                }
                conn.Close();
            }
        }

标签:c#,数据绑定,linq
0
投稿

猜你喜欢

  • C#知识整理

    2021-08-02 18:24:26
  • C#实现将汉字转化为2位大写的16进制Unicode的方法

    2022-03-11 21:45:07
  • C#通过指针实现快速拷贝的方法

    2022-09-26 01:43:08
  • 安卓逆向分析之酷狗signature案例分享

    2022-12-06 21:23:37
  • Android中Bitmap用法实例分析

    2023-03-21 11:06:31
  • Springboot中登录后关于cookie和session拦截问题的案例分析

    2022-09-25 19:26:44
  • 深入浅析C#泛型类型

    2023-01-30 06:45:54
  • Java创建随机数的四种方式总结

    2022-05-11 10:29:17
  • 详解Spring Boot自动装配的方法步骤

    2023-11-18 15:02:18
  • Android数据共享 sharedPreferences 的使用方法

    2023-06-19 01:43:07
  • Java中IO流文件读取、写入和复制的实例

    2023-08-12 03:23:29
  • C# 调用C++写的dll的实现方法

    2022-10-25 11:36:56
  • C#实现的上传图片、保存图片、加水印、生成缩略图功能示例

    2022-03-02 03:17:29
  • springboot 文件上传大小配置的方法

    2023-08-02 11:49:16
  • C#动态创建button的方法

    2023-02-11 15:58:26
  • Java实现单例模式的五种方法介绍

    2022-10-20 17:32:56
  • C#多线程之Thread中Thread.Join()函数用法分析

    2022-01-20 14:47:58
  • JPA 加锁机制及@Version版本控制方式

    2022-10-06 10:57:58
  • 解决WPF附加属性的Set函数不调用的问题

    2022-09-27 09:52:38
  • Java+Selenium实现文件上传下载功能详解

    2021-06-23 23:33:23
  • asp之家 软件编程 m.aspxhome.com