Use LINQ and C# to make a new List from an old List

。_饼干妹妹 提交于 2019-11-29 04:07:31
List<FillStruct> origList = ...
List<NewFillStruct> newList = origList.Select(x => new NewFillStruct {
    numlong = x.buy - x.sell, date = x.date
}).ToList();

However, note that struct is not necessarily a good choice for this (prefer class unless you have a good reason).

Or to avoid LINQ entirely:

List<NewFillStruct> newList = origList.ConvertAll(x => new NewFillStruct {
    numlong = x.buy - x.sell, date = x.date
});

This will select all your data from the enumerable "fillstructs" and create an enumerable of "NewFillStruct" containing the calculated values.

 var newfills = from fillstruct in fillstructs
               select new NewFillStruct
               {
                   numlong = fillstruct.buy - fillstruct.sell,
                   date = fillstruct.date
               };
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!