C# List object collection is reorganized into a new collection and a field is obtained to form a new array or string to separate

//实体集合
List<Question> list=new List<Question>();
//重组为需要的字段的新实体集合
var temp = list.Select(it => new { it.quesId,it.questype }).ToList();
//获取某个属性值组成数组,并已逗号隔开
var tempstr =string.Join(",", list.Select(it =>it.quesId ).ToList());
//或者
string resultSeries = list.Aggregate("", (current, s) => current + (s.Name + ","));

//或者for循环
string result = "";
for (int i = 0; i < list.Count; i++) {
    result = result + list[i] + ",";
}
Console.Write("循环方式实现结果: " + result);
//去掉最后一个","使用TrimEnd。
Console.Write("循环方式实现结果: " + result.TrimEnd(','));




Guess you like

Origin blog.csdn.net/qq_26695613/article/details/129694712