• C# LINQ & XML


    LINQ(Language-INtegrated Query) 是一种用作查找、存取数据库或XML文件的语言模式。

    是沟通面向对象语言与数据库的方式。与SQL很相似。

    using System;
    using System.Collections.Generic;
    using System.Linq;           //linq namespace
    namespace Programming_CSharp
    {
        // Simple customer class
        public class Customer           //定义customer对象,构成要搜索的集合
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string EmailAddress { get; set; }
            // Overrides the Object.ToString() to provide a
            // string representation of the object properties.
            public override string ToString()
            {
                return string.Format("{0} {1}
    Email:  {2}",
                            FirstName, LastName, EmailAddress);
            }
        }
    // Create a customer list with sample data
    private  static  List<Customer> CreateCustomerList()
    {
       List<Customer> customers = new List<Customer>
       {
          new Customer { FirstName = "Orlando“,LastName = "Gee",
                                        EmailAddress = "orlando0@adventure-works.com"},
          new Customer { FirstName = "Keith“, LastName = "Harris",
                                        EmailAddress = "keith0@adventure-works.com" },
          new Customer { FirstName = "Donna“, LastName = "Carreras",
                                        EmailAddress = "donna0@adventure-works.com" },
          new Customer { FirstName = "Janet“, LastName = "Gates",
                                        EmailAddress = "janet1@adventure-works.com" },
          new Customer { FirstName = "Lucy“, LastName = "Harrington",
                                        EmailAddress = "lucy0@adventure-works.com" }
        };
        return customers;
      }

    // 内存中构成Customer的集合

       static void Main()    // Main program
       {
           List<Customer> customers = CreateCustomerList();
           // Find customer by first name 
          IEnumerable<Customer> result =  from   customer in customers
          where  customer.FirstName == "Donna"  
          select customer;    //select 放在最后
          Console.WriteLine("FirstName == "Donna"");
          foreach (Customer customer in result)
             {    Console.WriteLine(customer.ToString());}  //显示结果
          customers[3].FirstName = "Donna";                 //修改数据
          Console.WriteLine("FirstName == "Donna" (take two)"); //显示结果
          foreach (Customer customer in result)
             {  Console.WriteLine(customer.ToString());}
        }
      }
    }//namespace

    Join 关键字

    内连关键字  将两个数据表连接连接

    [data source 1] join [data source 2] on [join condition]

    LINQ中含有关键字

    where  from  select等

    与SQL类似的   where表示筛选条件

    from 表示指定的范围

    select 表示查找映射

    Var关键字

    var通用数据类型  必须初始化

    var num = 1234;
    var strnum = "1234";
    var classsimple = new class1;
  • 相关阅读:
    工作流二次开发之新增表单实践(二)
    layui表格及工作流二次开发实践(一)
    记一个递归封装树形结构
    SpringCloud微服务之宏观了解
    统一结果返回&统一异常处理
    mybatis-Plus 实践篇之CRUD操作
    修改MariaDB-root密码
    iftop-监控服务器实时带宽情况
    Wordpress安装-报错说明
    MariaDB忘记root密码
  • 原文地址:https://www.cnblogs.com/yi-jie/p/4460978.html
Copyright © 2020-2023  润新知