LINQ: getting started with LINQ (1)-Introduction to LINQ query and linq Query

Source: Internet
Author: User

LINQ: getting started with LINQ (1)-Introduction to LINQ query and linq Query
Getting started with LINQ (1)-Introduction to LINQ Query

A query is an expression used to retrieve data from a data source. Over time, people have developed different languages for various data sources, such as SQL for relational databases and XQuery for XML. Therefore, developers have to learn new query languages for each data source or data format they must support. By providing a consistent model for using data across data sources and data formats, LINQ simplifies this situation. Objects are always used in LINQ queries. You can use the same encoding mode to query and convert data in XML documents, SQL databases, ADO. NET datasets,. NET collections, and any other formats available to their LINQ providers.

I. Three parts of the query operation 1. Operation trilogy: (1) obtain the data source (2) create a query (3) execute a query
1 internal class Program 2 {3 private static void Main (string [] args) 4 {5 // 1. obtain the data source 6 var nums = new int [7] {0, 1, 2, 3, 4, 5, 6}; 7 8 // 2. create query 9 var numQuery = 10 from num in nums11 where (num % 2) = 012 select num; 13 14 // 3. run the query 15 foreach (var num in numQuery) 16 {17 Console. writeLine ("{0}", num); 18} 19} 20}

 

The complete query operation is displayed. In LINQ, the query execution is completely different from the query itself. In other words, the query itself refers to creating only query variables without retrieving any data.

 

Ii. Data sources

In the previous example, because the data source is an array, it implicitly supports the generic IEnumerable <T> interface. The types that support IEnumerable <T> or derived interfaces (such as generic IQueryable <T>) are called queryable types.

The querytype can be used as a LINQ data source without modification or special processing. If the source data does not appear in the memory as a queryable type, the LINQ provider must represent the source data in this way. For example, you can load an XML document to the queryable XElement type in the LINQ to XML file:
1 // create a data source from XML 2 // using System. Xml. Linq; 3 var contacts = XElement. Load (@ "c: \ xxx. xml ");

In LINQ to SQL, you must first create an object relationship ing. Write a query for these objects, and then the communication with the database is processed by the LINQ to SQL statement at runtime. In the following example, MERS MERs indicates a specific table in the database.

1 var db = new Northwnd (@ "c: \ northwnd. mdf "); 2 3 // query customers in London 4 var custQuery = 5 from cust in db. customers6 where cust. city = "London" 7 select cust;

    

Iii. Query

Query the information to be retrieved from the data source. The query can also specify how to sort, group, and structure the information before it returns. The query is stored in the query variable and initialized with the query expression.

In the previous example, all the even numbers are returned from the integer array. The query expression contains three clauses: From, WhereAnd Select. (If you are familiar with SQL, you will notice that the order of these clauses is different from that in SQL .) FromClause specifies the data source, WhereClause specifies the application filter, SelectClause specifies the type of the returned element. Currently, you must note that in LINQ, the query variable itself does not perform any operations and does not return any data. It only stores the information necessary to generate results when a query is executed at a later time. Iv. query execution 1. delayed execution

As mentioned above, the query variable itself only stores the query command. The actual query execution will be delayedForeachStatement. This concept is called "delayed execution ".

2. Force Execution immediately

The query that executes Aggregate functions on a series of source elements must first access these elements cyclically.Count,Max,AverageAndFirstThis is the query type. Because the query itself must useForeachTherefore, these queries are not explicitly executed.ForeachStatement. Also note that these types of queries return a single value insteadIEnumerableSet.

var numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };var evenNumQuery =    from num in numbers    where (num % 2) == 0    select num;var evenNumCount = evenNumQuery.Count();

 

To forcibly execute any query and cache its results immediately, you can call the ToList <TSource> or ToArray <TSource> method.

1 var numQuery2 =2        (from num in numbers3         where (num % 2) == 04         select num).ToList();5 6 var numQuery3 =7       (from num in numbers8        where (num % 2) == 09         select num).ToArray();

In addition, you can placeForeachLoop to force the query. However, by callingToListOrToArrayYou can also cache all data in a single set object.

 

[Source] text mainly comes from Microsoft official documents and some images are also from Microsoft official documents.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.