将ObservableCollection拆分为有限数量的集合

问题描述 投票:0回答:1

我正在使用付款报告打印机进行工作,并且有一个ObservableCollection,可以在选择客户名称时进行填充。因此,例如,我有一个集合名称Clients,最多只能有60个条目。出于打印目的,我需要将其分成5个单独的列表,我想不起来该怎么做。

如果有帮助,这是所用物品的类别

public class payment
{
    public string amount { get; set; }
    public string date { get; set; }
}
c# observablecollection
1个回答
0
投票

我有一个集合名称Clients,最多只能有60个条目。为了打印,我需要将其分成5个单独的列表

我想这可能看起来像:

// An observable collection of 60 items
var clients = new ObservableCollection<Payment>(
    Enumerable.Range(0, 60).Select(i =>
        new Payment {Amount = i.ToString()}));

// The number of lists to create
var numLists = 5;
var itemsPerList = clients.Count / numLists + 1;

// Create a list of new ObservableCollection lists
var smallerCollections = new List<ObservableCollection<Payment>>();

// Copy the items from 'clients' into smaller collections of
// 'itemsPerList' size, and add those to the smallerCollections list
for (var i = 0; i < clients.Count; i++)
{
    // Add a new list every time we add 'itemsPerList' items
    if (i % itemsPerList == 0) smallerCollections.Add(new ObservableCollection<Payment>());

    // Add this item to the last list
    smallerCollections.Last().Add(clients[i]);
}
© www.soinside.com 2019 - 2024. All rights reserved.