有类 class User 包含两个属性 Id,Name,现在获得List<User> Users =new List<User>{ new User{Id=1,Name="张三"},new User{Id=2,Name="李四"}};现在我想获得Users的Id字符串"1,2",使用Aggregate, Users.select(u=>u.id).Aggregate((x,y)=>x+","+y)),请教这个select可以省略的写法
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
Users.Aggregate((x,y)=>x.id+","+y.id))
可以用
string result1 = userList.Aggregate("", (a, b) =>
{
return a + b.Id + ",";
});
result1 = result1.Trim(',');
,但是这个写个要判断最后的分隔号;
可以用下面的写法
string result2 = string.Join(",", userList.Select(a => a.Id));