C# LINQ & XML

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

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

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}\nEmail:  {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的集合spa

 

   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 关键字code

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

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

 

LINQ中含有关键字blog

where  from  select等内存

与SQL相似的   where表示筛选条件get

from 表示指定的范围string

select 表示查找映射

 

Var关键字

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

var num = 1234;
var strnum = "1234";
var classsimple = new class1;
相关文章
相关标签/搜索