SubSonic Forums
All Your Database Are Belong To Us

Random ActiveRecord Collection

Latest post 09-23-2008 11:54 AM by matrixIII. 7 replies.
  • 09-21-2008 6:43 PM

    Random ActiveRecord Collection

    I need an ActiveRecord Collection of 2 objects that are selected at Random from the Student table where the FirstName is "John".

    So, this is what I have so far
    //The following gives me all the objects where FirstName is "John" -- but I need only 2 randomly selected records
    StudentCollection _StudentCollection = new StudentCollection().Where(Student.FirstName, "John").Load(); 

    //Other part I might need to use is the Query object
    Query _Query = new Query():
    _Query.Top = 2;
    _Query.OrderBy = OrderBy.Asc("newid");

    How do I combine the two so I can use the query object? Or is there is different way of doing what I am trying to achieve?

    Thanks for any help in advance!

  • 09-21-2008 7:38 PM In reply to

    Re: Random ActiveRecord Collection

    Reply |Contact |Answer

     StudentCollection s = new SubSonic.Select.From("Student")
                         .Where("FirstName")
                         .IsEqualTo("John")
                         .Top("2")
                         .OrderAsc("NewId")
                         .ExecuteAsCollection<StudentCollection>();

    But, that doesn't solve your "Random" issue.

    For tthat you could either use the RND function in a SQLServer stored procedure or return all rows and write c# logic to select 2 random rows.

     

  • 09-21-2008 9:42 PM In reply to

    Re: Random ActiveRecord Collection

    Actually, what he is doing should solve it, BUT, I think the Orderby Clause should be:

    .OrderAsc("NewID()")

    That will cause the SQL Server to generate a random GUID for each row, and order it by that, and the TOP 2 prunes off the 2 as expected.

    The NewID is a function, not a field, so I think you have to include the paranthesis.

    That solves the random issue.

    Software Is Like Cathedrals: First we build 'em, then we pray.
  • 09-21-2008 9:47 PM In reply to

    • ranomore
    • Top 10 Contributor
    • Joined on 11-05-2007
    • Salt Lake City
    • Posts 322

    Re: Random ActiveRecord Collection

    NewID() is a function, not a column. If the query Geoff suggested works, it should return random results.

  • 09-21-2008 9:58 PM In reply to

    Re: Random ActiveRecord Collection

     

    ranomore:
    NewID() is a function, not a column

    I stand corrected by DiverKas and ranmore Yes

    In which case, I agree, the query I posted should return 2 random rows

     

  • 09-22-2008 11:20 AM In reply to

    Re: Random ActiveRecord Collection

    Thanks everyone for you help! I really appreciate it!

    GeoffAtDatagaard, The NewId() works for randomness but "Top" is coming up as undefined. Any suggestions?

    Thanks

  • 09-23-2008 5:59 AM In reply to

    Re: Random ActiveRecord Collection

     Top is a member of Select but not Query.

    Which one are you using and have you got the current release ?

     

  • 09-23-2008 11:54 AM In reply to

    Re: Random ActiveRecord Collection

    Thank you. I had to move "Top" to the beginning as it is a member of Select and not SqlQuery. "OrderAsc", "Where" all return SqlQuery thats why it wasent finding it. The below works!

    StudentCollection s = new SubSonic.Select().Top("2")
                         .From("Student")
                         .Where("FirstName")
                         .IsEqualTo("John")
                         .OrderAsc("NewId")
                         .ExecuteAsCollection<StudentCollection>();

     

Page 1 of 1 (8 items) | RSS