我有列表,customer并希望它id在单独的列表中List<int>.
我试过用:
customerList.Cast<int>.Distinct().ToList()
Run Code Online (Sandbox Code Playgroud)
但它不起作用.我也想要不同的客户列表.
我怎么能用LINQ语法呢?我的查询应该做些什么改变?
roo*_*roo 21
试试这个:
List<int> customerIds = customerList.Select(c => c.Id).Distinct().ToList();
Run Code Online (Sandbox Code Playgroud)
该代码假定您的customer对象具有一个返回int的Id属性.
Hab*_*bib 10
从您的客户列表中选择ID,然后使用ToList获取ID列表.
var IdList = customerList.Select(r=> r.ID).ToList();
Run Code Online (Sandbox Code Playgroud)
要获得不同的ID,请尝试:
var IdList = customerList.Select(r=> r.ID).Distinct().ToList();
Run Code Online (Sandbox Code Playgroud)