[C #3.0 experience] the built-in LINQ and xlinq [dlinq] extension methods in orcas

Source: Internet
Author: User

 

Everyone knows that LINQ is with orcas (visualstudio2007. net released a built-in extension method, in fact, the entire namespace is a library of extension methods, it allows developers to query any data type. Everyone thinks this is the trend of Data Query. Maybe the SQL language will develop slowly in the future, but what I want to see most now is the warmth and happiness that LINQ brings to us. Microsoft put these extension methods in system. in the LINQ namespace, when we create a new type, you will find one more in the default namespace. net2.0 and earlier versions have only three namespaces, and now there is an additional system.. LINQ defines a standard extension method for query operators, which can be used by. NET developers to easily query XML, relational databases,. Net objects, and any other data structure types. Let's go. Let's take a look at the joy that LINQ brings to us! (Vsmar07ctp)

Next, let's take a look at the different types of data queried by LINQ. However, because vsmar07ctp has a bug, we should first remove this bug when looking at the example below. When I checked M $ connect, I found someone submitted this bug, Which is web. however, it is very easy to delete the configuration. We can open the web. in config, find the <compiler> node and comment out the subnode. After OK removes the bug, We can compile and debug the node as follows:

1 < System . Codedom >
2 < Compilers >
3 < Compiler Language = "C #; CS; CSHARP" Extension = ". Cs"
Type = "Microsoft. CSHARP. csharpcodeprovider, system, version = 2.0.0.0,
Culture = neutral, publickeytoken = b77a5c561934e089" />
4
5 <! -- <Provideroption name = "compilerversion" value = "v3.5"/>
6 </Compiler> -->
7 </ Compilers >
8 </ System. codedom >
9

 

 

Since the time limit is not long enough for my writing, let's take a look at the Extension Method for. Net objects and XML using LINQ. As for the database, let's take a look at it in the next time. First, we create a new class. because it gives a more intuitive effect, I directly clip the graph. However, posting on a blog is not a good practice,CodeAs follows:

Here, the class simplification in net3.0 is not much mentioned. We don't have to write hidden variables, just like we can define an object class above. We can use the new class initializer of. Net 3.0 and the Set initializer to initialize it. The Code is as follows:

During initialization, we will be given a great smart reminder, so writing code is much less than dotnet2.0.

Then, I can use the standard "where ()" extension provided by system. LINQ to obtain the "person" objects whose age is 24 in this collection. The Code is as follows:


Many methods have been extended by LINQ. As you can see in the smart prompt, this code returns:

Name: Xiaobing; nickname: wangsoul Xiaobing;
Name: Xiao song; nickname: SEK;
Name: xiaosun; nickname: Follow the cloud with the wind;

The following is the smart prompt interface:

 


We can also understand a lot of new functions, but no other examples are shown here. Let's take a look at the application of the LINQ Extension Method in XML. In fact, we can also apply them to data searches such as databases. First, we need to define an XML file. The Code is as follows:

1 <? XML version = "1.0" encoding = "UTF-8" ?>
2 < People >
3 < Person Userid = "1" >
4 < Name > Xiaobing </ Name >
5 < Nickname > Cyber soul soldier </ Nickname >
6 < Age > 24 </ Age >
7 </ Person >
8 < Person Userid = "2" >
9 < Name > Riley </ Name >
10 < Nickname > Qqing </ Nickname >
11 < Age > 23 </ Age >
12 </ Person >
13 </ People >
14

There are multiple methods for retrieving XML data. Here we will look at how to use LINQ to retrieve data. To use this function, of course, we need to introduce the namespace system. XML. (this is also the legendary xlinq. By the way, for the database, dlinq is located in system. data. in the name of the namespace ). [Note: if you cannot find these two spaces, you can directly reference the DLL class library in 3.5 Under the % WINDOWS % \ Microsoft. NET \ framework directory.]

 

1 Xdocument people = Xdocument. Load (server. mappath ( " People. xml " ));
2 Ienumerable < Xelement > Person = People. descendants ( " Person " )
3 . Where (P => P. Attribute ( " Userid " ). Value =   " 1 " );
4

The code above returns a sequence of "xelement" elements. xelemen is an XML node element of no type. We can use the select () Extension Method of LINQ to construct the data shape and provide a Lambda expression that uses the new object initializer syntax to fill in the same "person" class:

1 Xdocument people = Xdocument. Load (server. mappath ( " People. xml " ));
2
3 Ienumerable < Person > Person = People. descendants ( " Person " )
4 . Where (P => P. Attribute ( " Userid " ). Value =   " 1 " )
5 . Select (P =>   New Person {userid = Convert. toint32 (P. Attribute ( " Userid " ). Value ),
6 Name = P. element ( " Name " ). Value,
7 Nickname = P. element ( " Nickname " ). Value,
8 Age = Convert. toint32 (P. element ( " Age " ). Value)
9 });
10

As I am also a beginner, today's experience is here. At last, I always want to express my overall feelings. The functions that LINQ brings to us are not worth mentioning, the power of LINQ allows a common query programming model and syntax for all data types (databases, XML files, objects in memory, and web-services; LINQ can be combined to allow developers to easily add new methods/operators to the query syntax. We can apply our own extended methods to them. LINQ is extensible, it can be used with any data provider type and can be combined with the existing ORM engine ...... But I think the extension method will make me feel messy when prompted intelligently. If you don't want to use LINQ, we recommend that you remove the system. LINQ namespace to make the intelligent prompt clearer. Other functions and effects will be continued later, that is to say, you want to know what to do and listen to the next decomposition!

 

 

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.