Change uncertainty to confirmation ~ Do I have to sort the data in a random way by using LINQ to SQL?

Source: Internet
Author: User

Back to directory

In general, in the era of T-SQL, random sorting can generate a guid value for newid,CodeGenerally:

Select   Top   10   *   From User Order   By   Newid ();

In the era of LINQ to SQL, because the generated statements are delayed (this may be the reason), you can use the code similar to this:

 
FromUserInDB. User order by guid. newid ()SelectUser

It cannot be implemented. If you need to find an implementation method for your LINQ to SQL, you can add a function in the current data context class: (Note: For scalability, create a score

)

 1  ///   <Summary>  2       ///  Data context Extension  3       ///   </Summary> 4       Public   Partial   Class  Dbdatacontext  5   {  6           ///   <Summary>  7           ///  Random sorting  8           ///   </Summary>  9          ///   <Returns> </returns>  10 [Function (name = "  Newid  " , Iscomposable = True  )]  11           Public  Guid newid ()  12   {  13               Return (Guid )(This . Executemethodcall ( This  , (Methodinfo) (methodinfo. getcurrentmethod ())))
. Returnvalue )); 14 15 } 16 }

Specific extension code:

 
FromUserInDB. User order by DB. newid ()SelectUser

If you want to sort a string in the order of characters, you can use indexof. The Code is as follows:

1List <String> Productids = {"1","2","3"};2LINQ. tolist (). orderbydescending (A =>Productids3. Indexof (A. mc_productid ))4. Tolist ();

Sometimes, we need to sort multiple fields in primary and secondary order. At this time, we can use the thenby that comes with LINQ, which is relatively simple and will not be used as an example.

Back to directory

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.