【问题标题】:How can i fill 1D array from 2D array and how can i sort 1D array?如何从二维数组中填充一维数组以及如何对一维数组进行排序?
【发布时间】:2010-06-20 19:20:02
【问题描述】:

感谢大家帮助我的白痴问题(看看我之前的帖子:))。但我确实需要下面。我应该从每个单元格数据表的长度填充 1 维数组。并对一维数组 linq 进行排序,也没有 linq

 int[][] lengths;

            using (DataTable table = GetTable())
            {
                lengths = (from DataRow row in table.Rows
                           select
                           (from DataColumn col in table.Columns
                            select row[col].ToString().Length).ToArray()).ToArray();
            }

            int[] Sortedlist;
            foreach (int[] row in lengths)
            {
                Sortedlist = row; ---- I NEED HELP !!!!
            }

            foreach (int item in Sortedlist)
            {
                item.Sort(); ----- I NEED HELP!!!
            }

My Data:

static DataTable GetTable() { // // Here we create a DataTable with four columns. // DataTable table = new DataTable(); table.Columns.Add("Dosage", typeof(int)); table.Columns.Add("Drug", typeof(string)); table.Columns.Add("Patient", typeof(string)); table.Columns.Add("Date", typeof(DateTime)); // // Here we add five DataRows. // table.Rows.Add(25, "Indocin", "David", DateTime.Now); table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now); table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now); table.Rows.Add(21, "Combivent", "Janet", DateTime.Now); table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now); return table; }

【问题讨论】:

    标签: c# .net visual-studio visual-studio-2008 linq


    【解决方案1】:

    不确定你的意思。您有一个 int[][] 数据结构(来自主 linq 查询)。 您的要求是将其转换为 int[] 结构,然后使用 linq 对其进行排序,并使用 Array.sort 对其进行排序。 应该这样做

       int[] UnSortedlist = lengths.SelectMany(x => x).ToArray();
                int[] sortedListLinq = UnSortedlist.OrderBy(x => x).ToArray();
                Array.Sort(UnSortedlist);
                int[] sortedListnonLinq = UnSortedlist;
    

    【讨论】:

    • 我喜欢:Array.Sort(UnSortedlist); int[] sortedListnonLinq = UnSortedlist;但是我怎样才能像 Reverse() 方法一样使用降序呢?
    猜你喜欢
    • 2017-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-24
    • 2021-04-14
    • 2017-05-25
    • 1970-01-01
    • 2012-04-03
    相关资源
    最近更新 更多