2
我目前正在從事一個項目,其中一個要求是能夠通過web服務向供應商下訂單。 一整天都會有不同的人將物品添加到購物袋,然後在工作日結束時,訂單將被放置。Linq分爲平均分組
當訂單交付時,倉庫中的人員將物品解包,並檢查匹配的交貨單。
爲了使這項工作更容易,他們希望將訂單分成更小的子訂單,每個子訂單最多15件。
這是我第一次嘗試一個ttackling說:
var groupingBySupplierID = shoppingBag.Where(x => x.SelectedSupplier != null).GroupBy(x => x.SelectedSupplier.SupplierID))
var groupedListOf15Items = groupingBySupplierID
.Select((item, index) => new {item, index}) //Items with the index in the list
.GroupBy(x => x.index/15) //Integerdivison on the index.. 1..14/15 == 0. 15..29/15 == 1 and so on
.Select(x => x.Select(y => y.item)); //Get the items back out from the groups
但是,如果有,說17個項目,即時得到的IEnumerable<IEnumerable<ShoppingBagItem>>
爲15的計數,然後1 2項。
理想情況下,我想要返回X列表,其中每個項目的數量均勻分佈,在本例中,分別爲9和8。
任何想法,我怎麼能做到這一點?
我知道有一些簡單的解決方案,我失蹤了..謝謝! – Frank 2011-05-28 15:59:26