2016-11-24 4 views
0

私は以下のように設定コンボボックスがあります。C#の値

private void SiteChanged(object sender, SelectionChangedEventArgs e) 
{ 
    if (comboBoxSites.SelectedIndex == -1) return; 

    Site site = comboBoxSites.SelectedItem.Value as Site; 

    comboBoxDetector.Items.Clear(); 
    if (site != null) 
    { 
     foreach (Detector detector in site.Detectors) 
     {     
       comboBoxDetector.Items.Add(new ComboBoxItem() 
       { 
        Content = string.Format("{0} ({1})", detector.Track.TrackName, detector.DetectorID), 
        Tag = detector 
       }); 
     } 
    } 
    if (comboBoxDetector.Items.Count > 0) 
     comboBoxDetector.SelectedIndex = 0; 

    btnShow_Click(null, null); 
} 

さて、これは私のコンボボックスに正しい情報を示しているが。
しかし、私はコンテンツ文字列に余分なものを1つ追加したいと思います。

私は最初にクエリを追加してみました。

foreach (Detector detector in site.Detectors) 
    { 
     LoadOperation<DetectorType> loadOp = context.Load(context.GetEnabledDetectorTypesQuery(detector.DetectorID)); 

     comboBoxDetector.Items.Add(new ComboBoxItem() 
     { 
      Content = string.Format("{0} ({1})", detector.Track.TrackName, detector.DetectorID), 
      Tag = detector 
     }); 
    } 

今、私は、クエリを追加して、それがすべてのエラーを与えていません。
クエリを追加した後、私のコードは次のようになります。
しかし、私は、クエリから結果を取得したいと思います。だから私はこのコードを追加しました:

foreach (Detector detector in site.Detectors) 
    { 
     LoadOperation<DetectorType> loadOp = context.Load(context.GetEnabledDetectorTypesQuery(detector.DetectorID)); 
     DetectorType type = loadOp.Entities; //Added this 

     comboBoxDetector.Items.Add(new ComboBoxItem() 
     { 
      Content = string.Format("{0} ({1}) {2}", detector.Track.TrackName, detector.DetectorID, type.Description), 
      Tag = detector 
     }); 
    } 

今説明は表示したい列です。しかし、DetectorType type = loadOp.Entities;はエラーを与えている:cannot implicitly convert type

は私がコンボボックスにDescription値を表示することができるように方法はありますか?

+0

このエラーは明らかに** loadOp.Entities **の型を暗黙的に変換できないと言っています** DetectorType **へ。それが同じタイプであることを絶対に確信しているなら、** DetectorType type =(DetectorType)loadOp.Entities; ** –

+0

@ m.rogalskiを使用して明示的にタイプを変換することができます。別の方法とそれを呼び出す、それは私に結果を示しています。また、あなたが言ったものに変更すると、私は同じエラーになります。 – Mitch

答えて

0

loadOp.EntitiesはIEnumerable<DetectorType>となります。返されたエンティティが1つしかない(または最初のエンティティのみに関心がある)場合は、DetectorType type = loadOp.Entities.FirstOrDefault();