views:

59

answers:

1
public class Mailing
{
    public string To { get; set; }
    public string AttachmentPath { get; set; }
    public int AttachmentCount { get; set; }
}

giving this structure i have a IList<Mailing> mailingList where To has dups and the AttachmentPath is unique per To.

i need to send emails to the mailingList where each unique To has multiple attachments (ie AttachmentPaths).

this partially works -- i can get the attachment count and iterate and add attachements as i need but i think this is ugly. i also need to limit a single email per group of attachments where the below completely breaks.

var investorMailings = (from m in mailingList
                       group m by new
                               {
                                   m.GroupBy,
                                   m.To
                               }
                           into g
                           select new DistintInvestorMailing
                               {
                                   To = g.Key.To,
                                   AttachmentCount = g.Count()
                               }).ToList();

any ideas?

+2  A: 
var groupedMailings = mailingList.GroupBy(g => g.To);
var investorMailings = groupedMailings.Select(
    g => new 
    { 
        To = g.Key,
        Attachments = g.Select(k => k.AttachmentPath),
        Count = g.Sum(j => j.AttachmentCount) 
    }
);

Should Produce an IEnumerable of annonymous types with the following properties:

  • .To The address to send to
  • .Count The number of attachments to send
  • .Attachments an IEnumerable of strings referencing the AttachmentPath's

Note: Yes this can be turned into 1 linq statement, but i broke it into two for clarity purposes

Aren
very cool -- thanks
CurlyFro