The EntitySpaces Community

Share and learn about the EntitySpaces Architecture.
Welcome to The EntitySpaces Community Sign in | Join | Help
in
Home Forums Photos

suggestions on loading a random result?

Last post 08-13-2008, 9:22 AM by David.Parsons. 14 replies.
Sort Posts: Previous Next
  •  01-05-2008, 12:48 AM 7460

    suggestions on loading a random result?

    for unit testing i'd love to load a random result from a table... any suggestions without pulling the entire collection over?

  •  01-05-2008, 8:09 AM 7462 in reply to 7460

    Re: suggestions on loading a random result?

    If your database supports paging, then you could do it with dynamic query, otherwise, I think you will have to write and call a custom stored procedure, or at least load a collection of primary keys, pick one via random indexer, and LoadByPrimaryKey:

    Code:
    OrderDetails od = new OrderDetails();
    
    od.Query.es.CountAll = true;
    od.Query.es.CountAllAlias = "Count";
    od.Query.Load();
    
    int totalRowCount = (int)od.GetColumn("Count");
    Random autoRand = new Random();
    
    od = new OrderDetails();
    
    od.Query.OrderBy(od.Query.OrderID, esOrderByDirection.Ascending);
    od.Query.OrderBy(od.Query.ProductID, esOrderByDirection.Ascending);
    od.Query.es.PageSize = 1;
    od.Query.es.PageNumber = autoRand.Next(totalRowCount - 1) + 1;
    od.Query.Load();

    David Neal Parsons
    www.entityspaces.net
  •  01-22-2008, 8:36 AM 7741 in reply to 7462

    Re: suggestions on loading a random result?

    I'm trying to achieve the same thing using this select statement in MySQL:

    SELECT *

    FROM tablename

    ORDER by RAND()

    limit 1

    How would i pass that through EntitySpaces?

    Thanks

    Ryan 

  •  01-22-2008, 9:51 AM 7745 in reply to 7741

    Re: suggestions on loading a random result?

    Try passing in as a string this to your OrderBy() on the Query, pass "<RAND()>" complete with the < > brackets

    EntitySpaces | Twitter | BLOG | Please honor our Software License
  •  01-22-2008, 1:39 PM 7751 in reply to 7741

    Re: suggestions on loading a random result?

    Neat trick, Ryan and Mike.

    Code:
    OrderDetails od = new OrderDetails();
    
    od.Query.es.Top = 1;
    od.Query.OrderBy("<RAND()>", esOrderByDirection.Ascending);
    
    od.Query.Load();

    LastQuery:

    Code:
    SELECT * FROM `northwind`.`order details` 
      ORDER BY RAND() ASC LIMIT 1

    David Neal Parsons
    www.entityspaces.net
  •  01-22-2008, 1:55 PM 7752 in reply to 7751

    Re: suggestions on loading a random result?

    BTW, while syntactically correct for SQL Server as well, that only works for MySQL because, at the server, MySQL is doing the random ORDER BY before it does the LIMIT. SQL Server just gets the first row, and then randomly orders it... Not too useful.

    SQL Server LastQuery:

    Code:
    SELECT  TOP 1 * FROM [Northwind].[dbo].[Order Details] 
      ORDER BY RAND() ASC

    David Neal Parsons
    www.entityspaces.net
  •  01-23-2008, 4:47 AM 7755 in reply to 7752

    Re: suggestions on loading a random result?

    In SQL Server this command makes the trick.

    SELECT TOP 1 * FROM [Northwind].[dbo].[Order Details] ORDER BY newid()

  •  01-23-2008, 7:42 AM 7760 in reply to 7755

    Re: suggestions on loading a random result?

    Cool, paschoal1. This does work for SQL Server, where RAND does not.

    Code:
    OrderDetails od = new OrderDetails();
    
    od.Query.es.Top = 1;
    od.Query.OrderBy("<NEWID()>", esOrderByDirection.Ascending);
    
    od.Query.Load();

    David Neal Parsons
    www.entityspaces.net
  •  02-03-2008, 10:10 PM 7897 in reply to 7760

    Re: suggestions on loading a random result?

    I've been looking at this a bit more, and the suggestion I made above using RAND in MySQL is very slow for large datasets, whereas David's suggestion is apparently the faster way of doing it.Thanks for all the suggestions for SQL server too.
  •  02-09-2008, 1:32 PM 7976 in reply to 7897

    Re: suggestions on loading a random result?

    Es Team:

    Why not create esOrderByDirection.Randomily to do this?

    Of course, it is necessary to investigate other databases to see if it is possible.

    Thanks.

  •  02-13-2008, 5:21 AM 8048 in reply to 7976

    Re: suggestions on loading a random result?

    I find another way to do this in sql server that involves three queries an uses the function Round. It works.

    Maybe it is faster then the other way.

    DECLARE @mx int, @mn int
    SELECT @mx = MAX(idAssessor) FROM assessor
    SELECT @mn = MIN(idAssessor) FROM assessor
    SELECT * FROM assessor WHERE idAssessor = ROUND(@mn + (RAND() * (@mx-@mn)),0)

  •  08-12-2008, 12:18 PM 10698 in reply to 7760

    Re: suggestions on loading a random result?

    This code isn't working for me.  I think it's because the Order By clause gets surrounded with hard brackets like this...

    ... Order By [<NEWID()>] Asc


  •  08-12-2008, 4:07 PM 10700 in reply to 10698

    Re: suggestions on loading a random result?

    I just double-checked this using ES2008 against SQL Server Northwind. It does not put square brackets around raw SQL, and works as expected.

    Code:
    OrderDetails od = new OrderDetails();
    
    od.Query.es.Top = 1;
    od.Query.OrderBy("<NEWID()>", esOrderByDirection.Ascending);
    
    od.Query.Load();
    

    LastQuery:

    Code:
    SELECT  TOP 1 * 
    FROM [Order Details] 
    ORDER BY NEWID() ASC

    Perhaps, if you posted the code for your query, we might spot something. When you post your reply, the last icon on the right will let you paste in source code, and format it for the selected language. Although, after you click "OK", you will probably have to hand-edit "&lt;" and replace it with "<".


    David Neal Parsons
    www.entityspaces.net
  •  08-13-2008, 5:52 AM 10719 in reply to 10700

    Re: suggestions on loading a random result?

    Thank you for double checking.  We have not yet upgraded to ES2008 (the wheels of state government often turn slow).  I'll assume that's the issue for now and will find a workaround.

    Thanks again!
     

  •  08-13-2008, 9:22 AM 10727 in reply to 10719

    Re: suggestions on loading a random result?

    The ability to use raw SQL in an OrderBy was not added until ES 2007.1.1119.0. Prior to that, the in-line raw SQL notation could only be used in a Select clause.
    David Neal Parsons
    www.entityspaces.net
View as RSS news feed in XML