【问题标题】:Sorting and categorising data from database对数据库中的数据进行排序和分类
【发布时间】:2017-05-06 17:15:45
【问题描述】:

我需要一种方法来对来自数据库的大量(大约 120 个或更多值)数据进行分类。什么都没有想到,所以我决定在这里问。我需要一个C#SQL 解决方案(或至少提示它)。

目前需要排序的数据在一列中,但我愿意接受任何建议。数据示例如下:

- A2-11 (Main branch that contains all A2 from below)
- A2-113
- A2-114
- A2-115
- .
- .
- .
- F-L-5 (Main branch)
- F-L-55 (Another branch in there)
- F-L-56 (Contains all values below)
- F-L-566 
- F-L-567
- .
- .
- .

提前致谢!!

可以作为例子展示,小树图:

TreeExample

【问题讨论】:

  • 用您正在使用的数据库标记您的问题。

标签: c# sql oracle sorting


【解决方案1】:

尝试自定义 OrderBy

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;


namespace ConsoleApplication50
{
    class Program
    {

        static void Main(string[] args)
        {

            DataTable dt = new DataTable();
            dt.Columns.Add("Category", typeof(string));


            string[] data =  { "Main-A2-11", "Main-A2-113", "Main-A2-114", "Main-A2-115",
                              "Third-F-L-56", "Third-F-L-566", "Third-F-L-567",
                              "Second-F-L-5", "Second-F-L-55"
                            };

            foreach (string d in data)
            {
                dt.Rows.Add(new object[] { d });
            }

            dt = dt.AsEnumerable().GroupBy(x => x.Field<string>("Category").Contains("-") ? 
                     x.Field<string>("Category").Substring(0,x.Field<string>("Category").IndexOf("-")) :
                     x.Field<string>("Category"))
                .Select(x => x.OrderBy(y => new SortRecord(y,"Category"))).SelectMany(x => x).CopyToDataTable();

        }
    }
    public class SortRecord : IComparer<SortRecord >, IComparable<SortRecord>
    {
        string[] rowSplitArray;
        int length = 0;
        public SortRecord(DataRow x, string name)
        {
            rowSplitArray = x.Field<string>(name).Split(new char[] { '-' });
            length = rowSplitArray.Length;
        }


        public int Compare(SortRecord  rowA, SortRecord rowB)
        {

            int len = Math.Min(rowA.length, rowB.length);

            for (int i = 0; i < len; i++)
            {
                if (rowA.rowSplitArray[i] != rowB.rowSplitArray[i])
                    return rowA.rowSplitArray[i].CompareTo(rowB.rowSplitArray[i]);
            }

            return rowA.length.CompareTo(rowB.length);

        }
        public int CompareTo(SortRecord row)
        {
            return Compare(this, row);
        }
    }

}

【讨论】:

  • 我刚刚意识到它并不能完全回答我的问题。您的解决方案对数据进行了排序,但是,我也想对其进行分类。我的数据不会有“Main”、“Second”或其他任何内容,而是给出的格式:- F-L-56; - F-L-566; - F-L-567;我怎么能使用你已经写过的东西(@jdweng)并以主分支包含 F-L-56、F-L-566 和 F-L-567 的方式进行分类。可以将其可视化为菜单中的子菜单。
  • 我期待您提出这个额外的请求。已经有了答案。更新了代码。
【解决方案2】:

也许使用Hierarchyid? 大卫

【讨论】:

    【解决方案3】:

    有多种方法可以实现结果,但如果您还需要决定性能,请按照本文进行操作。What are the options for storing hierarchical data in a relational database?

    你当然可以在sql中使用hierarchyid。 https://docs.microsoft.com/en-us/sql/relational-databases/hierarchical-data-sql-server

    This 可能对我有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-03
      • 1970-01-01
      • 2022-07-07
      相关资源
      最近更新 更多