2013-10-23 53 views
0
private void GetRecDept() 
{ 
    cmbDept.Items.Clear(); 
    ListViewMeasurements.Items.Clear(); 
    SFCDataContext SFC = new SFCDataContext(); 
    try 
    { 
     var query = (from i in SFC.Systems_SettingsMeasurements 
        orderby i.RowID descending 
        select new { 
         RowID = i.RowID, 
         Measure = i.Measurement, 
         Target = i.TargetPercentage, 
         Dept = i.Department_ID         
        }); 
     foreach (var w in query) 
     { 
      ListViewItem List = new ListViewItem(w.RowID.ToString()); 
      List.SubItems.Add(w.Measure); 
      List.SubItems.Add(string.Format("{0:n2}", w.Target)); 
      List.SubItems.Add(w.Dept); 
      ListViewMeasurements.Items.AddRange(new ListViewItem[] { List }); 
     } 
     foreach (var r in query) 
     { 
      cmbDept.Items.Add(r.Dept.Distinct()); 
     } 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message.ToString()); 
    } 
}     

及其對cmbDept.Items.Add()輸出表示這種串的 System.Linq.Enumerable+<DistinctIterator>d__7a 1 [System.Char]`不像在列表視圖 我怎樣才能將其轉換爲一個字符串類型,可讀取到 用戶它在一個組合框中。實現C#的LINQ鮮明幫助以組表中的列

回答

2

試試這個:

var distinctDept = query.Select(x=>x.Dept).Distinct().ToArray(); 
cmbDept.Items.AddRange(distinctDept) 

關於您的問題,Dept是字符串,當您使用cmbDept.Items.Add(r.Dept.Distinct());添加的項目是IEnumerable<char>,當然它不能顯示你所料,ToString()被稱爲然後顯示IEnumerable<char>並按照您的描述給出結果。