将列表项分配给相同类型但具有添加属性的新列表

时间:2018-08-29 10:13:10

标签: c# list

我有var client = new RestClient("myurl"); var request = new RestRequest(Method.GET); request.AddHeader("authorization", **AUTHKEY**); return client.Execute(request).Content; 类型的列表。我也有一种Notification

带分数的通知如下:

NotificationWithScore

我想使用现有的现有通知列表创建类型为public class NotificationWithScore { public Notification Notification { get; set; } public int Score { get; set; } } 的新列表,并提供默认分数0。

当我创建NotificationWithScore

我只能使用foreach分别分配它们。在Linq中有办法做到这一点吗?

1 个答案:

答案 0 :(得分:1)

使用LINQ

List<NotificationWithScore> AddScore(List<Notification> list)
{
    return list.Select(n => new NotificationWithScore { Notification = n, Score = 0 }).ToList();
}