2016-12-08 4 views
0

私のDBは次のようになります。リストは、その順序変更を続けている

Conversationモデルは次のようになります
public class BrContext:DbContext 
{ 
public DbSet<Conversation> AllConversations { get; set; } 
public DbSet<ChatReference> ChatReferences { get; set; } 
public DbSet<Product> Products { get; set; } 
} 

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; } 

    } 

マイChatReferenceモデルは次のようになります。

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

    } 

私はプロパティとして 'CurrentChatReference'のリスト{"AllReferences"}を持っていますDBに保存されているモデルで実行します。

DBの値を見るとプロジェクトのデバッグに時間が掛かることがあります - 私は、データベース内の最新の会話のリスト「AllReferences」のChatReferencesの順序が入れ替わっていることがわかりますまわり。

なぜこのようなことが起こっているのでしょうか?

答えて

1

バックエンドクエリが結果を返すとき、それらの結果はリストに設定されます。 OrderByを使用してクエリを送信しない場合、結果は常に同じ順序で取得されることが保証されないため、List内の項目は常に同じ順序ではありません。 OrderBy句を使用して結果を取得するか、結果をモデルに入力した後に結果をソートします。

Entity Framework Ordering Includes

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

+0

あなたの助けをいただき、ありがとうございます。あなたが送った上記のリンクを見ました:「Entity Framework Ordering Includes」とその指示に従いました。 – Anonymous

関連する問題