2010-04-22 62 views
3
public class InvestorMailing 
{ 
    public string To { get; set; } 

    public IEnumerable<string> Attachments { get; set; } 

    public int AttachmentCount { get; set; } 

    public long AttachmentSize { get; set; } 
} 

我有一个IList<InvestorMailing> mailingList。如果附件大小大于x,那么我需要将我的对象分成块。有没有一个简单的方法来做到这一点?如何根据某些属性将对象分块为块?

编辑:

这是如何我产生我的邮件:

 var groupedMailings = mailingList.GroupBy(g => g.GroupBy); 

     var investorMailings = groupedMailings.Select(
      g => new DistinctInvestorMailing 
      { 
       Id = g.Select(x => x.Id).FirstOrDefault(), 
       To = g.Key.Trim(), 
       From = g.Select(x => x.From).FirstOrDefault(), 
       FromName = g.Select(x => x.FromName).FirstOrDefault(), 
       Bcc = g.Select(x => x.Bcc).FirstOrDefault(), 
       DeliveryCode = g.Select(x => x.DeliveryCode).FirstOrDefault(), 
       Subject = g.Select(x => x.Subject).FirstOrDefault(), 
       Body = g.Select(x => x.Body).FirstOrDefault(), 
       CommentsOnStatus = g.Select(x => x.CommentsOnStatus).FirstOrDefault(), 
       Attachments = g.Select(x => x.AttachmentPath), 
       AttachmentCount = g.Select(x => x.AttachmentPath).Count(), 
       AttachmentSize = g.Sum(x => x.AttachmentSize), 
       MailType = g.Select(x => x.MessageType).FirstOrDefault() 
      } 
     ).ToList(); 
+0

你想拆分什么? InvestorMailings的列表或个人InvestorMailing的附件? – dtb 2010-04-22 17:34:09

+0

InventorMailing是一个对象,每个对象都有自己的AttachmentCount属性。你什么时候想拆分?即使列表中的单个项目大于x,您还想分割吗? – azamsharp 2010-04-22 17:35:16

+0

几乎相同http://stackoverflow.com/questions/2678008/how-to-split-linq-grouping – 2010-04-22 18:18:16

回答

1

它应该是相当简单的用一个标准的方法来做到这一点。考虑下面这个例子:

class Foo 
{ 
    public Foo(int weight) { Weight = weight; } 
    public int Weight { get; set; } 
} 

...

IEnumerable<IList<Foo>> GroupFoosByWeight(IList<Foo> foos, int weightLimit) 
{ 
    List<Foo> list = new List<Foo>(); 
    int sumOfWeight = 0; 

    foreach (Foo foo in foos) 
    { 
     if (sumOfWeight + foo.Weight > weightLimit) 
     { 
      yield return list; 
      sumOfWeight = 0; 
      list.Clear(); 
     } 

     list.Add(foo); 
     sumOfWeight += foo.Weight; 
    } 

    if (list.Count > 0) 
     yield return list; 
} 

...

List<Foo> foos = new List<Foo>() 
{ 
    new Foo(15), new Foo(32), new Foo(14), new Foo(19), new Foo(27) 
}; 

foreach (IList<Foo> list in GroupFoosByWeight(foos, 35)) 
{ 
    Console.WriteLine("{0}\t{1}", list.Count, list.Sum(f => f.Weight)); 
} 

编辑

我在它的工作一点,产生了LINQ版本。在这种情况下,它并不能真正节省大量代码,但这是一个开始。

int weightLimit = 35; 
int fooGroup = 0; 
int totalWeight = 0; 

Func<Foo, int> groupIncrementer = f => 
{ 
    if (totalWeight + f.Weight > weightLimit) 
    { 
     fooGroup++; 
     totalWeight = 0; 
    } 

    totalWeight += f.Weight; 

    return fooGroup; 
}; 

var query = from foo in foos 
      group foo by new { Group = groupIncrementer(foo) } 
       into g 
       select g.AsEnumerable(); 

foreach (IList<Foo> list in query) 
{ 
    Console.WriteLine("{0}\t{1}", list.Count, list.Sum(f => f.Weight)); 
} 
+0

这工作 - 谢谢 – CurlyFro 2010-04-23 14:24:07

0

是:

var highs = mailingList.Where(i => i.AttachmentSize > 10000).ToList(); 
var lows = mailingList.Where(i => i.AttachmentSize <= 10000).ToList(); 

你怎么需要从这个打破它们分开放在一边?

HTH。

+0

我*想*他想分批处理这些东西。因此,一旦他超过了某个给定的阈值,请执行操作并继续。所以它不是真正的个人规模,而是集体。 – 2010-04-22 18:05:20

1

这里有一个办法做到这一点使用一些LINQ发现,有足够的空间留给添加附件一大块:

var chunks = new List<List<InvestorMailing>>(); 
int maxAttachmentsSize = 10; 

foreach (InvestorMailing mail in mailingList) 
{ 
    var chunkWithSpace = chunks 
     .Where(list => list.Sum(x => x.AttachmentSize) + 
         mail.AttachmentSize <= maxAttachmentsSize) 
     .FirstOrDefault(); 

    if (chunkWithSpace != null) 
    { 
     chunkWithSpace.Add(mail); 
    } else { 
     chunks.Add(new List<InvestorMailing> { mail }); 
    } 
} 

结果存储在chunks

相关问题