单独排序两个日期列和累积效果?

时间:2017-05-25 12:23:40

标签: c# entity-framework linq

我有两个日期列和两个大小列(一个大小列与一个日期列相关),如下表所示。现在我想要两个数组,在第一个数组中它将按Collected排序并显示CollectedSize的累积效果,与Staged和StagedSize相同。

必需:

收集的数组|
1/1/2016 | 1
11/1/2016 | 4
12/1/2016 | 6
30/1/2016 | 11

分阶阵列|
13/1/2016 | 3
14/1/2016 | 7
18/1/2016 | 13个
2016年1月16日20

表:

|收集| CollectedSize |上演| StagedSize |

| 11/1/2016 | 3 | 14/1/2016 | 4

| 12/1/2016 | 2 | 13/1/2016 | 3

| 30/1/2016 | 5 | 18/1/2016 | 7

| 01/1/2016 | 1 | 2016年1月16日6

目前使用以下代码:

public class ProductionDataOverTimeVM
{
    public ProductionDataOverTimeVM()
    {
        Collected = new List<TimeChartXAxis>();
        Staged = new List<TimeChartXAxis>();
    }
    public List<TimeChartXAxis> Collected { get; set; }
    public List<TimeChartXAxis> Staged { get; set; }
}
public class TimeChartXAxis
{
    public string x { get; set; }
    public string y { get; set; }
}

var queryResults = context.Datasets.ToList();
ProductionDataOverTimeVM obj = new ProductionDataOverTimeVM();
long? collectedBytes = 0;
long? Staged = 0;
foreach (var dataset in queryResults.OrderBy(d => d.Collected))
{
    if (dataset.Collected != null)
    {
        collectedBytes = collectedBytes + dataset.CollectedSize;
        obj.Collected.Add(new TimeChartXAxis
        {
            x = dataset.Collected != null ? BasicHelpers.FromUTCDate(dataset.Collected, parms.Offset).Value.ToString("dd/M/yyyy") : null,
            y = BasicHelpers.FormatBytesToSpecificFormat(collectedBytes, format, false)
        });
    }
}
foreach (var dataset in queryResults.OrderBy(d => d.Staged))
{
    if (dataset.Staged != null)
    {
        Staged = Staged + dataset.StagedSize;
        obj.Staged.Add(new TimeChartXAxis
        {
            x = dataset.Staged != null ? BasicHelpers.FromUTCDate(dataset.Staged, parms.Offset).Value.ToString("dd/M/yyyy") : null,
            y = BasicHelpers.FormatBytesToSpecificFormat(Staged, format, false)
        });
    }
}

enter image description here

这样做的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

怎么样?
var arrayofOrderByString = new []{"Collected","Staged"}

foreach(var key in arrayofOrderByString){
var y=0;
SortList<Datasets>(queryResults, key, SortDirection.Descending);
queryResults.foreach(s =>{
y=s.GetType().GetProperty(key).GetValue(s, null);
obj.Collected.Add(new ProductionDataOverTimeVM{
x =BasicHelpers.FromUTCDate(s.GetType().GetProperty(key).GetValue(s, null), parms.Offset).Value.ToString("dd/M/yyyy"),
y=collectedBytes
})
})
}

public void SortList<T>(List<T> list, string columnName, SortDirection direction)
{
    var property = typeof(T).GetProperty(columnName);
    var multiplier = direction == SortDirection.Descending ? -1 : 1;
    list.Sort((t1, t2) => {
        var col1 = property.GetValue(t1);
        var col2 = property.GetValue(t2);
        return multiplier * Comparer<object>.Default.Compare(col1, col2);
    });
}