c# 第六課 linq

來源:互聯網
上載者:User

標籤:

 

LINQ (Language-INtegrated Query)
Programmers perform every day is finding and retrieving (存取) objects in memory, a database, or an XML file.
SQL(relational model) can only search relational database, not object-oriented languages.
LINQ is a bridge over  object-oriented languages and relational database.
LINQ is SQL-like, and remove the distinctions(區別) among searching an in-memory data collection, a database, or an XML document. Integrated into C#.

using System;using System.Collections.Generic;using System.Linq;namespace Programming_CSharp{    // Simple customer class    public class 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);        }    }


這段代碼簡單的定義了一個隊列。

Language-integrated Query (LINQ) (LINQ) 是 Visual Studio 2008 和 .NET Framework 3.5 版中引入的一項創新功能,它在對象領域和資料領域之間架起了一座橋樑。                   

傳統上,針對資料的查詢都是以簡單的字串表示,而沒有編譯時間類型檢查或 IntelliSense 支援。             此外,您還必須針對以下各種資料來源學習一種不同的查詢語言:SQL 資料庫、XML 文檔、各種 Web 服務等等。  LINQ 使查詢成為 C# 和 Visual Basic 中的一流語言構造。  您可以使用語言關鍵字和熟悉的運算子針對強型別化對象集合編寫查詢。 

 

 

 

 

Deferred(延期的) Query Evaluation(1)


The declaration and initialization  of a query expression do not actually execute the query

 

 

IEnumerable<Customer> result = from   customer  in customers  where  customer.FirstName == "Donna“   select  customer;

Instead, a LINQ query is executed, or evaluated, when you iterate through the query result:

static void Main(){   List<Customer> customers = CreateCustomerList();    List<Address>  addresses = CreateAddressList();   // Find all addresses of a customer   var result = from customer in customers     join address in addresses on string.Format("{0} {1}",      customer.FirstName,customer.LastName) equals address.Name      orderby customer.LastName, address.Street descending     select new { Customer = customer, Address = address };     foreach (var ca in result)     {         Console.WriteLine(string.Format("{0}\nAddress: {1}",             ca.Customer, ca.Address));      }}

var關鍵字   可以賦予局部變數推斷“類型”var 而不是顯式類型。var 關鍵字指示編譯器根據初始化語句右側的運算式推斷變數的類型。推斷類型可以是內建類型、匿名型別、使用者定義型別、.NET Framework 類庫中定義的類型或任何錶達式。
上面的資料有點抽象不好理解.
樣本:
原來我們定義變數,是要這樣:
資料類型 變數名 = 值;
如:
int a = 1;
string b = "2";
也就是說,"必須先明確地"指定你的變數是什麼資料類型,才能給它賦值.這點很重要,要記住才好比較.
現在在C# 3.0裡,有了變化,就是可以不用像上面那樣定義變數了.
如:
var a =1 ;
那這個a,是什麼類型呢?和原來的做法不一樣了.
奧妙就在這裡,IDE或編譯器會根據你給a 的值:1,來"推論,斷定"a是一個整數類型.
同理:
var b = "2";
因為給b的值是"2"這樣一個字串,所以,b就是string類型...

Grouping and the group Keyword
An other powerful feature of LINQ, commonly used by SQL programmers but now integrated into the language itself, is grouping。

var result = from address in addresses      group address by address.Name;foreach (var gp in result){      Console.WriteLine("{0}", gp.Key);       foreach (var a in gp)             Console.WriteLine("\t{0}", a); }

運用group關鍵字。group    子句返回一個 IGrouping<TKey,TElement>對象序列,這些對象包含零個或更多個與該組的索引值匹配的項.

 

可以用 group 子句結束查詢運算式,如下面的樣本所示:

// Query variable is an IEnumerable<IGrouping<char, Student>>var studentQuery1 =    from student in students    group student by student.Last[0];

 

c# 第六課 linq

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.