Today, we have been tossing around for a long time to implement the select top 1 * From _ TABLE order by newid () SQL statement using LINQ!
There are several solutions available on the Internet, the most classic of which is:
1 // Add 2 [function (name = "newid", iscomposable = true)] 3 Public guid newid () 4 {5 return (guid) to the automatically generated mapping code) (This. executemethodcall (this, (methodinfo) (methodinfo. getcurrentmethod ()))). returnvalue); 6} 78 // in use 9var customer = (from C in CTX. customers orderby CTX. newid ()). first ();
Reference: http://www.cnblogs.com/Mirricle/archive/2007/08/16/858260.html
It is a pity that it is a LINQ to SQL, and I cannot find a common practice under. edmx, and it is said that once the model is updated, the entire modification will be overwritten. It is impossible for me to copy it once every modification, right?
The second method is:
(From P in dB. Products). orderby (E => guid. newguid (). Take (10 );
However, an error is reported when writing to entity! There are no bird methods such as guid. newguid () in the LINQ to entities class. It does not work.
Reference: http://www.cnblogs.com/hanxianlong/archive/2007/11/06/951462.html
The third method is found on the bird's Forum:
You can do it with esql
Model. createquery <Products> ( |
@ "Select value source. Entity |
From (select entity, sqlserver. newid () as Rand |
From products as entity) as source |
Order by source. Rand "); |
On this code you can use take (20) to get a list of random items, or you can just add "Top 20" to the SELECT statement
Reference: http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/9d41ee63-8656-4bc5-875e-3ef8dd3d8ee9
I typed it and reported an error during running, but it still didn't work.
The fourth method is:
First, create the following view:
Create view randomview
As
Select newid () as ID
Then create the following function that uses the View:
Create Function getnewid
(
)
Returns uniqueidentifier
As
Begin
Return (select ID from randomview)
End
The view is required because it's not possible to directly useNewidIn a scalar function.
You can then mapGetnewidUser-Defined Function using LINQ to SQL's function attribute. Again, see Chapter 8 for the details.
That's it! You can now write LINQ queries as usual. Here is an example to pick a random object:
VaR tool = dB. Tools. orderby (t => dB. getnewid (). First ()
Here is another example that usesGetnewidTo sort results randomly:
VaR tools =
From tool in db. Tools
Orderby dB. getnewid ()
Select tool. Name;
Reference: http://crazythief.com/KB/496509ca-6231-473b-b9c8-6bd87fc607ec
I did it again, then updated the model, and then looked for it. I just couldn't find the bird method getnewid.
I thought it was hopeless. I came back and asked about Rei. He said he wanted to write an icomparer. But I am a newbie and I don't know how to write it. Just ask if there is something simple? How about the view? The result shows that the fifth method can be used.
Method 5:
Add view-create view myview as select T. *, newid () as R from mytable t order by R;
Call method-var query = from Q in context. myview select Q;
Execute the conversion-XXXXXXXX.
OK. After a day, I finally solved the problem and continued to design my graduation project...