【问题标题】:Sort Multi Dimensional/Jagged Array from Api从 Api 排序多维/锯齿状数组
【发布时间】:2016-01-05 18:23:55
【问题描述】:

除了目前在堆栈上找到的帮助之外,我还需要一点帮助。

我有一个看起来像这样的数组(首先是多维数组还是锯齿状数组?)

其次,我想按 [X][4] 的开始日期对其进行排序

我已经尝试了几次搜索并看到了我厌倦的以下内容

//string[][] SenorityList = SalesEmployees.OrderBy(inner => inner[0][4]).ToArray();

但我真的不明白它是如何工作的,所以不能让它工作......

我还看到了http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=151,它看起来可以通过使用一个类来工作,但又不理解它,所以不知道如何根据我的需要部署它。

下面我添加了我正在使用的构建数组的导出,以便您可以看到变量名称等。

#region GrabSalesEmployees
        DateTime Now = DateTime.Now;
        EP203000Content EP203000 = context.EP203000GetSchema();
        context.EP203000Clear();
        string[][] SalesEmployees;
        SalesEmployees = context.EP203000Export(
                new Command[] {
                    EP203000.EmployeeInfo.ServiceCommands.EveryEmployeeID,
                    EP203000.GeneralInfoEmployeeSettings.EmployeeClass,
                    EP203000.EmployeeInfo.Status,
                    EP203000.EmployeeInfo.EmployeeID,
                    EP203000.EmploymentHistory.Position,
                    EP203000.EmploymentHistory.StartDate,
                    EP203000.EmploymentHistory.EndDate
                },

                new Filter[] {
                    new Filter { Field = new Field { FieldName = EP203000.GeneralInfoEmployeeSettings.EmployeeClass.FieldName }, Condition = FilterCondition.Equals, Value = "SALES", Operator = FilterOperator.And },             
                    new Filter { Field = new Field { FieldName = EP203000.EmployeeInfo.Status.FieldName }, Condition = FilterCondition.Equals, Value = "Active", Operator = FilterOperator.And },                                     
                    new Filter { Field = new Field { FieldName = EP203000.EmployeeInfo.EmployeeID.FieldName }, Condition = FilterCondition.NotEqual, Value = "BA00000450", Operator = FilterOperator.And },
                },

                    0, false, false
            );

【问题讨论】:

    标签: c# arrays multidimensional-array acumatica jagged-arrays


    【解决方案1】:

    锯齿状数组是一个array of arrays。如果您确定每个内部数组在第 4 个元素中都包含日期,则可以使用下一个代码:

    // for each element of external array (1st dimension) order by 4th element of jagged (2nd dimension) by ascending
    string[][] SenorityList = SalesEmployees.OrderBy(innerArray => innerArray[4]).ToArray();
    

    当然更好的方法是检查元素并将它们转换为 DateTime:

    string[][] SenorityList = SalesEmployees.OrderBy(innerArray =>
            {
                if (innerArray.Length >= 5)
                {
                    DateTime startDate;
                    if (DateTime.TryParse(innerArray[4], out startDate))
                        return startDate;
                }
                // if you want that unpasrsed dates will be on the end of the list use DateTime.MaxValue
                return DateTime.MaxValue;
            }).ToArray();
    

    【讨论】:

    • 效果很好,谢谢。 out starDate 有什么作用?
    • 它只是匿名方法中的一个变量,就像您应用程序中的所有其他变量一样。使用 startDate 是因为 TryParse 方法有 out 参数,需要在上面的代码中声明。它用于保存数组元素的解析值。您可以将选择块中的所有代码提取到分离的方法中。
    猜你喜欢
    • 1970-01-01
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多