2012-11-27 159 views
2

我有customer的列表,並希望它的id在單獨的列表List<int>從列表<Customer>獲取列表<int>使用linq

我試着使用:

customerList.Cast<int>.Distinct().ToList() 

但它不到風度的工作。我也想要不同的客戶int列表。

如何使用LINQ語法來做到這一點?我的查詢應該做什麼改變?

+0

你可以顯示一些你已經嘗試過的代碼嗎,至少Customer類的屬性會很好 – wizzardz

回答

14

試試這個:

List<int> customerIds = customerList.Select(c => c.Id).Distinct().ToList(); 

的代碼假設你的客戶對象有它返回一個int id屬性。

8

從客戶列表中選擇ID,然後使用ToList獲取ID列表。

var IdList = customerList.Select(r=> r.ID).ToList(); 

爲了得到不同的IDS嘗試:

var IdList = customerList.Select(r=> r.ID).Distinct().ToList();