0

my db looks like this:

public class BrContext:DbContext
{
public DbSet<Conversation> AllConversations { get; set; }
public DbSet<ChatReference> ChatReferences { get; set; }
public DbSet<Product> Products { get; set; }
}

My Conversation Model looks like this:

public class Conversation
    {
        [Key]
        public int ConversationId { get; set; }
        public string ConverserName { get; set; }
        public List<ChatReference> AllReferences { get; set; }

        public ChatReference CurrentChatReference { get; set; }

        public bool IsDealtWith { get; set; }

    }

My ChatReference Model looks like this:

public class ChatReference
    {
        public int ChatReferenceId { get; set; }
        public string ChatReferenceTime { get; set; }
        public string ChatReferenceContent { get; set; }
        public bool IsR { get; set; }

    }

as you see - I have a list {"AllReferences"} of 'CurrentChatReference' as a property in a model that is saved in the DB.

I have times in the course of debugging the project , that when i look at the values in the DB - i see that the order of the ChatReferences in the list 'AllReferences' in the latest conversation in the db has been switched around.

Does anyone have an idea of why this is happening?

Anonymous
  • 21
  • 9

1 Answers1

1

When backend query is returning the results, those results are populated in the List. If you are not sending the query using OrderBy, the results are not guaranteed to always come in the same order, so the items in the List are not always in the same order. Either you retrieve the results using OrderBy clause or Sort the results after you populate the results into your model.

Entity Framework Ordering Includes

How to Sort a List<T> by a property in the object

Community
  • 1
  • 1
Vinod
  • 1,882
  • 2
  • 17
  • 27
  • Thank you for your help. i looked at the above link you sent: "Entity Framework Ordering Includes" and followed their instructions and it really worked!!! – Anonymous Dec 08 '16 at 21:19