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__7a1[System.Char]` 与列表视图不同,我如何将其转换为用户可读的字符串类型,它位于组合框中。

4

1 回答 1

2

尝试这个:

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

关于您的问题,Deptis 字符串,当您使用cmbDept.Items.Add(r.Dept.Distinct());添加的项目时IEnumerable<char>,它当然无法按您的预期显示,ToString()在显示之前调用 theIEnumerable<char>并为您提供所描述的结果。

于 2013-10-23T08:37:11.980 回答