none
Keep Order RRS feed

  • Question

  • Hello,

    I am getting a question and its answers (options) for a poll and inserting into the database.

    When I display the form I would like the answers to appear at the same order as they were inserted.


    However, this is not happening ... In fact every time I create a poll, even with same answers, it seems its options are saved in some random order.


    Here is my code:


          Leaf.Options = Leaf.OptionsCSV.Split(
            new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(
            o => new OptionLeaf {
              Option = new Option {
                OptionId = Guid.NewGuid(),
                Answer = o.Trim(),
                PollId = pollBook.Leaf.Poll.PollId,
              }
            }).ToList();

          database.Options.InsertAllOnSubmit(Leaf.Options.Select(o => o.Option));


    OptionsCSV is a string that contains the options inserted in an input.


    For example:
    Yes,No,Maybe


    What is the best way to display the options using the same order as they were inserted?


    Thanks,
    Miguel
    Thursday, November 27, 2008 6:22 PM

Answers

  • Database tables are not ordered.  When you retrieve rows you are not guaranteed them back in any particular order.  Sometimes they may appear to be ordered by the primary key, but that is only due to some choice by the execution plan of the query and can change. 

     

    The only way to get data back in some specific order is to explicitly order it in your query.  So you could either order your options by id, answer value or some other value.  If you want them to always appear in some order not indicative in the data currently then you'll need to add some data to specify the order, like a sequence #, etc.

     

     

    Friday, November 28, 2008 7:10 PM
    Moderator