具有子foreach循环(嵌套foreach)的foreach循环的Lambda表达式并更改该值

时间:2014-05-23 06:31:22

标签: c# foreach lambda nested

我有两个类如下:

    public class Notification
    {
        public System.Guid NotificationId { get; set; }
        public Nullable<System.Guid> ClassId { get; set; }
        public Nullable<System.Guid> SentBy { get; set; }
        public string SenderName { get; set; }
        public Nullable<bool> Visible { get; set; }
        public string MessageText { get; set; }
        public Nullable<System.Guid> ClassViewRegId { get; set; }
        public string CreatedBy { get; set; }
        public Nullable<System.DateTime> CreatedDate { get; set; }
        public string LastModifiedBy { get; set; }
        public Nullable<System.DateTime> LastModifiedDate { get; set; }
        public List<MessageRecipient> Recipients { get; set; }
    }

    public partial class Recipient
    {
        public System.Guid NotificationRecipientId { get; set; }  
        public System.Guid NotificationId { get; set; }
        public string RecipientType { get; set; }
        public Nullable<System.Guid> RecipientId { get; set; }   
        public Nullable<System.Guid> ClassId { get; set; }
        public Nullable<System.Guid> ClassViewRegId { get; set; }
    }

现在我的代码列表中有另一个列表List。

所以我试图检查并更改下面的值:

 notify.Where(n => n.Recipients.Where(r => r.RecipientType == "Teacher").ToList().ForEach(s=>s.RecipientType="");

任何人请帮助我检查和更改子列表属性的值。

1 个答案:

答案 0 :(得分:3)

使用简单的foreach循环:

var teachers = notify.SelectMany(n => n.Recipients)
                     .Where(r => r.RecipientType == "Teacher");

foreach(Recipient teacher in teachers)
    teacher.RecipientType = "";