Analysis on Random Data Reading by LINQ

Source: Internet
Author: User

When learning about LINQ, you will often encounter the problem of Random Data Reading by LINQ. Here we will introduce the solution to the problem of Random Data Reading by LINQ.

Random Data Reading by LINQ

O/P mapping freely generated in the systemCodeAdd this method. If the O/P mapping code is compiled by the user or generated by the tool, the same is true. Here I will talk about my own. The System-generated LINQ to SQL class generates three files:. northwind. CS, northwind. dbml. layout, and. northwind. Designer. CS.

All we need to do is add the required method newid () in the northwind. Designer. cs. The function of this method is of course consistent with the newid () in the database.

The specific method code is as follows:

    1. [System. Data. LINQ. Mapping. databaseattribute (name = "northwind")]
    2. Public partial class northwinddatacontext: system. Data. LINQ. datacontext
    3. {
    4. Private Static system. Data. LINQ. mapping.
      Mappingsource = new attributemappingsource ();
    5. // Add it to the automatically generated mapping code
    6. [Function (name = "newid", iscomposable = true)]
    7. Public guid newid ()
    8. {
    9. Return (guid) (This. executemethodcall (this,
      (Methodinfo) (methodinfo. getcurrentmethod (). returnvalue ));
    10. }
    11. // The code generated later is omitted ..
 
 

Re-Generate and write this, and our access implementation will become very easy. The usage is consistent with the traditional access principle.

    1. DB = new northwinddatacontext ();
    2. VaR result = (from C in db. Customers orderby dB. newid () Select c). Take (10 );
    3. Foreach (VAR item in result)
    4. Console. writeline (item. companyName );
    5. Console. Readline ();
 
 

The preceding section describes how to read data randomly by using LINQ.

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.