分享

C# Linq

 昵称10504424 2013-11-18

LINQ- Language Integrated Query 语言集成查询

LINQ通过对象的方式对数据库进行描述。

LINQ是一种能够快速对大部分数据源进行访问和数据整合的一种技术,使用相同的基本查询表达式模式类查询和转换SQL数据库、ADO.NET数据集、XML文档和流已经.NET集合中的数据。

从.NET3.5开始引入LINQ

LINQ to Objects

LINQ to DataSet

LINQ to SQL

LINQ to Entities

LINQ to XML

命名空间

System.Data.Linq 该命名空间包含支持与LINQ to SQL应用程序中的关系数据库进行交互的类

System.Data.Linq.Mapping 该命名空间包含用于生成表示关系数据库的结构和内容的LINQ to SQL对象模型的类

System.Data.Linq.SqlClient 该命名空间包含与SQL Server进行通信的提供程序类,已经包含查询帮助器方法的类

System.Linq 该命名空间提供支持使用语言集成查询(LINQ)进行查询的类和接口

System.Linq.Expression 该命名空间包含一些类,接口和枚举,它们使语言级别的代码表达式能够表示为表达式树形式的对象

System.Xml.Linq 包含LINQ to XML的类,LINQ to XML是内存中的XML变成接口

查询表达式使用form1.cs

复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LinqTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };
            //创建查询
            //查询结果的类型是IEnumberable<int>
            var result0 = from num in numbers 
                         where (num % 2) == 0 
                         select num;
            //执行查询结果
            foreach (int num in result0)
            {
                listBox1.Items.Add(num);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            int[] numbers = new int[7] { 2, 1, 2, 6, 4, 5, 3 };
            var result1 =                       //声明查询
                          from num in numbers   //声明临时变量num,其值来源于numbers
                          where num>3           //当前值大于3
                          orderby num descending//按照descending方式排序,降序
                          select num;           //将当前项加入到结果序列中
            foreach (int num in result1)
            {
                listBox1.Items.Add(num);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            int[] numbers = new int[7] { 2, 1, 2, 6, 4, 5, 3 };
            var result2 =                       //声明查询
                          from num in numbers   //声明临时变量num,其值来源于numbers
                          where num > 3           //当前值大于3
                          orderby num ascending//按照ascending方式排序,升序
                          select string.Format("当前项值为{0}",num);           //将当前项加入到结果序列中
            foreach (string num in result2)
            {
                listBox1.Items.Add(num);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            int[] numbers = new int[7] { 2, 1, 2, 6, 4, 5, 3 };
            int result3 =                       //声明查询
                          (from num in numbers   //声明临时变量num,其值来源于numbers
                          where num > 3           //当前值大于3
                          select num).Count();           //将当前项加入到结果序列中  
            button4.Text += "--" + result3.ToString();
        }
    }
}
复制代码

Form1.cs

1

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约