Re: Linq. Select.

Tech-Archive recommends: Fix windows errors by optimizing your registry



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#)
------------------------------------------------------------------------
.



Relevant Pages

  • Re: Is LINQ consumes double the time of Traditional Data Connectio
    ... Jon Skeet [C# MVP] wrote: ... there are some fundamental design decisions made in linq to sql ... Lead developer of LLBLGen Pro, the productive O/R mapper for .NET ... My .NET blog: http://weblogs.asp.net/fbouma ...
    (microsoft.public.dotnet.general)
  • Re: Ado.Net and Unicode (N)
    ... will be re-used) and they'll be secure (as no longer Sql injection ... manager might not get the funding we need to to this... ... Lead developer of LLBLGen Pro, the productive O/R mapper for .NET ... My .NET blog: http://weblogs.asp.net/fbouma ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Is Linq to SQL worthwhile
    ... I know what you mean but I don't think moving all your sql to linq is the answer. ... Lead developer of LLBLGen Pro, the productive O/R mapper for .NET ... My .NET blog: http://weblogs.asp.net/fbouma ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Ado.Net and Unicode (N)
    ... The only issue we've run into, on SQL Server, is the N ... Is it possible to add the N prefix ... Lead developer of LLBLGen Pro, the productive O/R mapper for .NET ... My .NET blog: http://weblogs.asp.net/fbouma ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Which identity?
    ... > Some postings I read suggested using MAXto retrieve the inserted record ... > each of these 3 SQL features? ... Another way to do it if using the .AddNew method on a Jet based ADO ... Dim rsTest As ADODB.Recordset ...
    (microsoft.public.access.queries)