Re: Linq. Select.
- From: "Frans Bouma [C# MVP]" <perseus.usenetNOSPAM@xxxxxxxxx>
- Date: Tue, 22 Apr 2008 01:09:56 -0700
Lasse Vågsæther Karlsen wrote:
shapper wrote:
Hello,
How do I select 10 random records from a table?
Thanks,
Miguel
The SQL way is to order by a GUID and select the top 10 rows.
Unfortunately, SQL is made to retrieve the rows you specify, not just
"any 10 rows, doesn't matter which ones".
I'm afraid you're going to have to come up with a way to specify the
10 random rows you want.
Could you for instance retrieve the primary key of all the rows, and
then grab 10 random items from that array and retrieve the full rows
for those keys?
the query is:
select top 10 * from customers order by newid() asc
so what should be done is calling newid() in the orderby clause of the
linq query. THis isn't mapped by default, but you can define your own
function mapping, so after that's done it should be possible to orderby
on that function call.
FB
--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
.
- Follow-Ups:
- Re: Linq. Select.
- From: Marc Gravell
- Re: Linq. Select.
- References:
- Linq. Select.
- From: shapper
- Re: Linq. Select.
- From: Lasse Vågsæther Karlsen
- Linq. Select.
- Prev by Date: Re: Refresh Linq Data Entities in the Model
- Next by Date: Re: ClickOnce again
- Previous by thread: Re: Linq. Select.
- Next by thread: Re: Linq. Select.
- Index(es):
Relevant Pages
|