2016-04-09 15 views
0

コレクションをソートし、そのコレクションに外部キーを使用したいと考えています。私は1:0 ... 1の関係を持って、それを私のMVCアプリケーションに表示します。外部キーを使用してorderby()を使用するにはどうすれば簡単ですか?

OutreachSet ==> 0:0 ... 1 ==> MotivationSet右ここ

public class OutreachSet 
     { 
      [Key] 
      [Display(Name = "Target Contact")] 
      public string TargetContact { get; set; } 
      public virtual MotivationSet MotivationSet { get; set; } 
     } 


public class MotivationSet 
    { 
     [Key, ForeignKey("OutreachSet")] 
     [Display(Name = "Target Contact")] 
     public string Name { get; set; } 

[Required] 
     public virtual OutreachSet OutreachSet { get; set; } 
    } 

var motivations = db.Motivations.Include(m => m.OutreachSet); 
var result = motivations.Select(s => s.OutreachSet).OrderByDescending(s => s.TargetContact); 

私はoderby基準として使用したいOutreachSetの内容が、ちょうどないTargetContactを取得します。それはcontext.Entryでそれを行うための最善の方法ですか?

私が持っている次の問題は、自分自身を無限に参照し続けるために、Quickwatch OutreachSetとMotivationSet semmを見るときです。

たとえば、これは:

var test = db.Entry(db.Motivations.First()).Entity.OutreachSet.TargetContact; 

私はそれをどのように並べ替えるのか分かりません。

答えて

1

あなたが達成しようとしていることははっきりしていないので、OutreachSetのコレクションを取得したいと仮定して、関連するMotivationSetTargetContractでソートします。

はそれを行うには、いくつかの方法がありますが、あなたの場合、最も簡単なのは、あなたのクエリにselectorder by句を交換するだけです:

var result = motivations.OrderByDescending(s => s.TargetContact).Select(s => s.OutreachSet); 
関連する問題