C# &引用;“膨胀”;阵列的任意一端,以满足大小要求

C# &引用;“膨胀”;阵列的任意一端,以满足大小要求,c#,arrays,linq,C#,Arrays,Linq,我有一组复杂的银行交易对象,它们被提炼成一个小数数组(如果需要,可以为空)。在前端,为了完全填充数据表,我需要一定数量的“事务总和”。但是,在某些情况下,没有足够的事务来填充数组大小,这会破坏UI 以下是构建数组的C#ViewModel代码的一部分: var minDate = this.Periods[0].FirstDayOfMonth(); // 11/1/2016 var maxDate = this.Periods[this.NumberOfPeriods - 1].LastDayOf

我有一组复杂的银行交易对象,它们被提炼成一个小数数组(如果需要,可以为空)。在前端,为了完全填充数据表,我需要一定数量的“事务总和”。但是,在某些情况下,没有足够的事务来填充数组大小,这会破坏UI

以下是构建数组的C#ViewModel代码的一部分:

var minDate = this.Periods[0].FirstDayOfMonth(); // 11/1/2016
var maxDate = this.Periods[this.NumberOfPeriods - 1].LastDayOfMonth(); // 11/20/2017

this.NetCashReceipts = this.NetCashReceiptsCredits
                .Where(bt => bt.Date.Between(minDate, maxDate, true))
                .GroupBy(bt => bt.Date.FirstDayOfMonth())
                .ToDictionary(group => group.Key, group => group.Sum(bt => bt.Amount))
                .OrderBy(kvp => kvp.Key)
                .Select(kvp => kvp.Value)
                .ToArray();
Select
语句之前,有序字典数据如下所示:

{System.Collections.Generic.KeyValuePair<System.DateTime, decimal>[11]}
    [0]: {[1/1/2017 12:00:00 AM, 27172.13]}
    [1]: {[2/1/2017 12:00:00 AM, 5080.18]}
    [2]: {[3/1/2017 12:00:00 AM, 63934.53]}
    [3]: {[4/1/2017 12:00:00 AM, 30006.93]}
    [4]: {[5/1/2017 12:00:00 AM, 64821.43]}
    [5]: {[6/1/2017 12:00:00 AM, 19161.06]}
    [6]: {[7/1/2017 12:00:00 AM, 130019.11]}
    [7]: {[8/1/2017 12:00:00 AM, 132176.08]}
    [8]: {[9/1/2017 12:00:00 AM, 72991.46]}
    [9]: {[10/1/2017 12:00:00 AM, 14440.88]}
    [10]: {[11/1/2017 12:00:00 AM, 18057.63]}
{System.Collections.Generic.KeyValuePair<System.DateTime, decimal>[11]}
    [0]: {[11/1/2016 12:00:00 AM, null]}
    [1]: {[12/1/2016 12:00:00 AM, null]}
    [2]: {[1/1/2017 12:00:00 AM, 27172.13]}
    [3]: {[2/1/2017 12:00:00 AM, 5080.18]}
    [4]: {[3/1/2017 12:00:00 AM, 63934.53]}
    [5]: {[4/1/2017 12:00:00 AM, 30006.93]}
    [6]: {[5/1/2017 12:00:00 AM, 64821.43]}
    [7]: {[6/1/2017 12:00:00 AM, 19161.06]}
    [8]: {[7/1/2017 12:00:00 AM, 130019.11]}
    [9]: {[8/1/2017 12:00:00 AM, 132176.08]}
    [10]: {[9/1/2017 12:00:00 AM, 72991.46]}
    [11]: {[10/1/2017 12:00:00 AM, 14440.88]}
    [12]: {[11/1/2017 12:00:00 AM, 18057.63]}
*
null
值将允许我用
N/a
填充前端数据表,而不是
0

,您只需执行以下操作:

List<decimal?> LNetCashReceipts = NetCashReceipts.ToList<decimal?>();
List lnetcashreipts=netcashreipts.ToList();

然后再做这个。

使用Linq没有简单的方法,但是Linq不是魔法,有人编写了所有这些扩展方法。如果你找不到一个适合你的需求,那么简单地实现一个适合你的需求

在这种情况下,您需要的是一个定制的
Take
,用于填充输出。填充端点总是比较容易,因此可以执行以下操作:

public static IEnumerable<TSource> TakeAndPadEnd<TSource>(
    this IEnumerable<TSource>source, 
    int count,
    Func<TSource, TSource> paddedValueSelector)
{
    var yielded = 0;
    var previous = default(TSource);

    using (var e = source.GetEnumerator())
    {
        while (e.MoveNext())
        {
            yield return e.Current;
            previous = e.Current;
            yielded += 1;

            if (yielded == count)
                yield break;
        }
    }

    while (yielded < count)
    {
        var newValue = paddedValueSelector(previous);
        yield return newValue;
        previous = newValue;
        yielded += 1;
    }
}

更新:删除元组解决方案,因为它是冗余的,上面显示的解决方案就足够了
TSource
可以是任何东西。

为什么要使用数组而不是
列表
或其他一般集合?因为
这个.netcashreipts
是一个
十进制数?[]
,而且设计决策早于我。我可以根据需要进行强制转换、更改等操作。您可以将属性更改为
列表
,这样就不必担心调整大小。或者更可能只是一个
列表
,不向其中添加空值。
this.NetCashReceipts = TakeAndPadd(
    12,
    this.NetCashReceiptsCredits
        .Where(bt => bt.Date.Between(minDate, maxDate, true))
        .GroupBy(bt => bt.Date.FirstDayOfMonth())
        .ToDictionary(
            group => group.Key, 
            group => group.Sum(bt => bt.Amount))
        .OrderBy(kvp => kvp.Key)
        .Select(kvp => kvp.Value),
    v => default(decimal?)).ToArray();