2010-04-22 47 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

相關問題