2015-10-14 46 views
0

我有一個記錄表,列出了我們客戶收到的所有更新。在該列中,公司代碼可以多次列出。我需要獲得列表中所有可用代碼的列表,但只顯示一次。獲取Combobox公司代碼的單個值列表

這裏是我的專欄:

enter image description here

我想查詢列,只返回:

DEM 
FRK 

而不是:

DEM 
DEM 
DEM 
FRK 

我可以查詢名單如下:

private void PopulateCompanyCodes() 
{ 
    var entity = new SuburbanWebServiceEntities(); 
    var qry = from x in entity.UpdateLogs 
      select x.CompanyCode; 

    comboBox_CompanyCodes.Items.Add(string.Empty); 
    foreach (var cc in qry.ToList()) 
    { 
    comboBox_CompanyCodes.Items.Add(cc); 
    } 
    comboBox_CompanyCodes.Text = string.Empty; 
} 

但當然,我越來越DEM多次列出:

enter image description here

有沒有辦法來得到第一個列表,只顯示一次的組合框每個項目?

謝謝!

+1

您需要使用distinct爲 http://stackoverflow.com/questions/5608127/distinct-in-linq – thinkindeveloper

回答

1

你有沒有試過這樣做。使用Distinct刪除重複項。通過

foreach (var cc in qry.Distinct().ToList()) 
+1

簡單而偉大的工作! tyvm! – ErocM

3

使用Distinct。你必須切換到method syntax,但這沒什麼大不了的。

var qry = entity.UpdateLogs.Select(l => l.CompanyCode).Distinct(); 

comboBox_CompanyCodes.Items.Add(string.Empty); 
foreach (var cc in qry.ToList()) 
{ 
    comboBox_CompanyCodes.Items.Add(cc); 
} 
0

用組:

var qry = entity.UpdateLogs.GroupBy(l => l.CompanyCode);