• C# Linq


    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

  • 相关阅读:
    chrome更新后,恢复本地丢失的书签和历史记录
    redis 集合set 使用 rediscluster 使用交集
    git 删除分支恢复
    SQL语句性能优化
    A调用B,b有事务,a没有
    Unable to tunnel through proxy. Proxy returns "HTTP/1.0 407 Proxy Authentica 问题处理
    fasnjson 转换
    String.format()的详细用法
    传递json
    基础入门-加密编码算法
  • 原文地址:https://www.cnblogs.com/Mysterious/p/3426847.html
Copyright © 2020-2023  润新知