How to Use NEWID () for self-learning and understanding in LINQ

Source: Internet
Author: User

The previous article discussed how to use the NEWID () Random Function in ms SQL. We will not discuss it here. in NET, LINQ is actually a profound combination of MSSQL database and object-oriented thinking. I personally understand it as an ORM. I have no in-depth research on ORM. I only understand the ideas here, also. Sorry, it seems that there are still a lot of things you need to learn by yourself.

First, you must add the following code to the DBML file to map the relational functions. In fact, the code is added to the DBML background code file. In the DESIGNER. CS file,

 

Code

 1   [Function(Name = "NEWID", IsComposable = true)]
2 public Guid NEWID()
3 {
4 return ((Guid)(this.ExecuteMethodCall(this,
5 ((MethodInfo)(MethodInfo.GetCurrentMethod()))).ReturnValue));
6 } 

 

 

The above code maps to the NEWID () system function in MSSQL.

Then, call the code file in your page FILE or form file. See the following CODE:

 

Code

     private void bindGridview()
{
db = new NorthWindDataContext();
var result = (from c in db.Customers orderby db.NEWID() select c).Take(10);

GridView1.DataSource = result;
GridView1.DataBind();
}

The preceding figure shows how to use NEWID in LINQ. Remember, if NEWID is the function name generated by you in the DBML code file, it must be capitalized when you call it automatically, this is defined by yourself, and does not have to have the same name as NEWID in MSSQL. You can write it as public Guid returnMssqlNewid () and so on, depending on how you can flexibly set the name, it is easier for others to read and call the API.

This article is written here. I hope you can make a lot of mistakes. A lot of communication.

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.