开发者

Linq Grouping query

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
   开发者_如何学Python                            {
                                   To = g.Key.To,
                                   AttachmentCount = g.Count()
                               }).ToList();

any ideas?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜