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