LINQ to object

Source: Internet
Author: User

Microsoft launched the LINQ Technology in. NET Framework 3.5,

LINQ-language integerated query (Language integration query ),

It mainly integrates queries on objects, dataset, SQL Server, and XML,

Of course, to query SQL Server, you need to explain that it is not named as LINQ to SQL Server,

Instead, it supports only SQL Server databases,

DB2 and Oracle are not supported yet.

First of all, let's talk about it from LINQ to object,

LINQ to object is a query operation performed on the object type using the LINQ technology,

However, not all objects can be queried using LINQ,

Only the set object that implements the ienumberable or ienumberable <t> Interface,

You can use the LINQ technology to complete the query operation,

In fact, in LINQ to dataset,

It also requires that datatable directly or indirectly implement the two interfaces mentioned above,

Let's take a look at a demo.

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. IO;

Namespace csharplanguage
{
Public partial class demo _ 5: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
// This is the simplest LINQ query technology.
// That is, LINQ to object
// However, this object must implement ienumerable or
// Set object of the ienumerable <t> Interface
// Otherwise, the query cannot be completed.
// Public class list <t>: ilist <t>, icollection <t>,
// Ienumerable <t>, ilist, icollection, and ienumerable
// The list <t> implements both the ienumerable and ienumerable <t> interfaces.
// Therefore, you can use the LINQ to object technology to perform query operations.

// Some new technologies added in C #3.0 are also used here,

// You can use the following method to initialize a collection:
List <string> mylist = new list <string> {"23", "baobeime", "Xiaozhen "};
Mylist. Add ("12.6 ");
Mylist. Add ("me ");
Mylist. Add ("perfect ");
Mylist. Add ("beautiful ");
VaR result = from P in mylist
Select P;
Foreach (VaR value in result)
{
Response. Write (Value + "-----");
}

// Use LINQ to object to query all the song files in the specified directory
// Specify the directory where the song is located
String dirpath = "F: \ music \ My Love ";

// Query the complete path of all the song files in the video.
// Here is an example of a typical LINQ to object
VaR music = from P in Getmusiclist (dirpath)
Where p. extension. tolower () = ". MP3"
Select P. fullname;
Foreach (VAR p in music)
{
Response. Write (P. tostring () + "<br/> ");
}

}

Private list <fileinfo> getmusiclist (string dirpath)
{
// Create a generic set object of the fileinfo type
List <fileinfo> Mymusiclist = new list <fileinfo> ();
// That is, the specified directory does not exist.
If (! Directory. exists (dirpath ))
{
Return NULL;
}
Else
{
Directoryinfo dirinfo = new directoryinfo (dirpath );
// Traverse all the files in this directory and add them to the generic set object
Foreach (fileinfo file in dirinfo. getfiles ())
{
Mymusiclist. Add (File );
}
Return mymusiclist;
}
}
}
}

The above is in the code-behind of the entire demo.CodeNow,

Attach one.

In fact, the demo is really easy. However, some of the key points involved must be mastered,

First, you need to be familiar with the basic expression of the LINQ statement,

The second is to specify the objects to be queried using LINQ to object,

At least one of ienumberable and ienumberable must be implemented.

2010-2-01

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.