I have a class like this:
public class MailMessage
{
public string From {get; set; };
public string To {get; set; };
public ICollection<Attachment> Attachments { get; set; }
}
public class Attachment
{
public string Name {get; set;}
public byte[] Content {get;set;}
}
I would like to get all attachments in Attachments collection whose name ends with .pdf.
I tried the following:
List<MailMessage> list = new List<MailMessage>();
List<attachment> pdfAttachmentsCollection = list.Where(x => x.Attachments
.Where(attachment =>
attachment.Name.EndsWith(".pdf")).Select(y => y.Content));
However, this doesnt work. Any suggestions. It gives error:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable ' to 'bool'
var pdfAttachmentsCollection = list.SelectMany(x => x.Attachments.Where(a => a.Name.ToLower().EndsWith(".pdf"))).ToList();list.SelectMany(x => x.Attachments). The rest is standardWherestuff that you'll work out easily. Jim's approach will work equally well.Name.EndsWith(".pdf", StringComparison.InvariantCultureIgnoreCase)