Using LINQ to implement queries on data objects

Source: Internet
Author: User

Because of the project needs, access to LINQ, after a period of learning, LINQ has a simple understanding of this topic to do a simple narrative.

First of all, you should write a few questions, from the macro to understand. what is LinQ? Why use LinQ? What are the benefits of using it?

           language-Integrated query ( language intergrated Query c# vb language extension, which allows the writing of c# vb code to manipulate memory data in the same way as querying the database. ---- Baidu Encyclopedia

The popular point is that it is querying data. So why use it to query data, what's a bit of using it?

        Let's consider this issue from a slightly abstract point of view. Object-oriented programming and fragmentation of data access: aobject-oriented languages and databases have two sets of completely unrelated data type systems, in programming languagesstring, in the database isvarchar, there is no uniformity whatsoever;SQLcoding experience behind, we writeSQLstatement, no prompt statement, only finished, just know whether it is correct, customer experience is poor;various query languages are inconsistent,XMLand the database have their own data query system, the object does not have its own query system. Based on all the above problems,LinQcame up.

The composition above also mentions thatLinQ includes three types of content for queries, objects,XML files, and databases. The query for the database is divided into three parts:

Linq to SQL ,

LINQ to DataSet ,

LINQ to Entities .


         query: linq query data in two ways, language query and method query sql statements in the form of queries, which are readable in a strong manner

LINQ several advanced query methods:

Aggregation: Max,min,count,sum and so on;

Sort by: Thenby,orderby and so on;

Partition: Take,skip,takewhile,skipwhile and so on;

Collection: Distinct

Generate: range,repeat

Here are a few examples of LinQ queries:

          1 Span lang= "en-US" style= "Font-family:calibri" >linq to Objects ):

<span style= "FONT-SIZE:18PX;" >private void Btnquery_click (object sender, EventArgs e) {//linq to Object Basic method section//Generic set number            According to person list<string> person = new list<string> (); Person.            ADD ("Boss"); Person.            Add ("Dick"); Person.            ADD ("Old Three"); Person.            ADD ("Old Four"); Person.            ADD ("Old Five"); Person.            ADD ("small Six"); Person.            ADD ("small Seven"); Person.            ADD ("small Eight");            Output person all elements//var result = from p in person select p; Output all people in the beginning of the person//statement, method Mixed mode, using a statement query, using the method to filter var result = (from P in person select p).            Where (P = = P.startswith ("old")); Print result.        Print (); } public static void Print (this ienumerable<string> IE) {//Get the traversed interface Ienumerator<str ing> result = ie.            GetEnumerator (); while (result. MoveNext ()) {Console.WriteLine ("\ n-----------------------\ n "); Console.WriteLine (Result.                Current);            Console.WriteLine ("\ n------------------------\ n"); }} </span>

            This code first defines a generic collection object person, and then uses query statements and query fee-based mixed mode to query out" old "people, and then print out. Query statements do not complete all of the query function, you need to use the Query method to filter, in this case the Query method is provided by the system. It can be seen that the query statement readability is strong, but the function is not strong enough, the query method is not readable, the function is more.

<span style= "FONT-SIZE:18PX;" >2 based on Query method pattern (LINQ to Objects) private void Btnselect_click (object sender, EventArgs e)        {            //linq to object Basic Method section            //generic collection data            person list<string> person = new list<string> ();            Person. ADD ("Boss");            Person. Add ("Dick");            Person. ADD ("Old Three");            Person. ADD ("Old Four");            Person. ADD ("Old Five");            Person. ADD ("small Six");            Person. ADD ("small Seven");            Person. ADD ("small Eight");    Method four calls the external complex method as the query condition           var result = person. Where (p = Judge (P));} public bool Judge (String s)        {            if (S.startswith ("old"))            {                return true;            }            else            {                return false;            }        </span>
This section of the code function as above, completely using the Query method to complete the filtering function, this method is different from the method in the previous example, this method is a redefined method.
<span style= "FONT-SIZE:18PX;" >3 Database-based query (LINQ to Entities) public ActionResult Index ()        {//1 using a LinQ statement to get Entity Data//query conditions used in lambda expression list< Models.blogarticle> list = (from B in db. blogarticle where B.aisdel ==false select B). ToList ();//2 uses ViewData to get data       viewdata["DataList"] = list;       3 Load View       list. Print ();}  public partial class blogarticle    {entity code: slightly     }</span>
<span style= "Background-color:rgb (255, 255, 255); >      </span><span style= "Background-color:rgb (255, 255, 255); font-family:arial, Helvetica, Sans-serif;" > </span>

In this case, we are simply replacing the data object with the entity collection, with the same basic syntax.

Advantages: Simple writing, easy to get started. Querying data in the form of objects, with the use of query statements and query methods, is a more intuitive data query.

Disadvantages: LINQ using the lazy load function, the pending data is put into memory, which consumes a lot of resources, resulting in wasted resources.


Using LINQ to implement queries on data objects

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.