2012-11-27 163 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();