【问题标题】:How to get records for top parent in a DataSet?如何获取数据集中顶级父级的记录?
【发布时间】:2021-04-27 15:28:08
【问题描述】:

我得到了一个数据集,其中包含以下格式的数据。

我需要返回给定类别的类别详细信息。如果该类别没有关键字,则需要返回顶级父级的关键字。顶级父级是父类别为-1的类别。

例如,对于类别 202,它应该返回 Teaching 作为关键字。和预期的输出应该是 一个逗号分隔的字符串。

ParentCategoryID=201,名称=操作系统,关键字=教学

我正在从下面的代码中获取记录。

string queryString = "SELECT [Category Id],[Parent Category Id],[Name],[Keywords] FROM [dbo].[Categories]";

SqlConnection sqlConnection = new SqlConnection(connectionString);
SqlDataAdapter adapter = new SqlDataAdapter(queryString, sqlConnection);

DataSet customers = new DataSet();
adapter.Fill(customers, "Categories");

我可以编写一个逻辑来从下面的代码中获取列表中的顶级父级。但是我无法按照数据集的要求来写。

public long GetParent(long category)
{
    var element = ls.FirstOrDefault(x => x.Category == category);

    if(element.ParentCategory == -1)
    {
        return category;
    }

    return GetParent(element.ParentCategory);
}

注意:表中可能有数百万条记录。

【问题讨论】:

  • 不清楚你需要什么。修改您的 sql 查询以在数据库中包含此逻辑或修改 DataSet,以便在必要时使用顶级类别中的关键字对其进行更新。如果前者你需要包含更多,从使用的 RDBMS 和所有表开始,包括它们的模式。如果是后者,您必须说明如果您只是循环所有行,为什么您的代码不起作用。
  • 我需要在 C# 中而不是在数据库中处理这个问题。

标签: c# asp.net .net asp.net-core .net-core


【解决方案1】:

尝试以下递归方法

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

namespace ConsoleApplication
{
    public class Program
    {
        static DataTable dt = null;
        public static void Main(string[] args)
        {
            dt = new DataTable();
            dt.Columns.Add("Category Id", typeof(long));
            dt.Columns.Add("Parent Category Id", typeof(long));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Keywords", typeof(string));

            dt.Rows.Add(new object[] { 100, -1, "Business", "Money" });
            dt.Rows.Add(new object[] { 200, -1, "Tutoring", "Teaching" });
            dt.Rows.Add(new object[] { 101, 100, "Accounting", "Taxes" });
            dt.Rows.Add(new object[] { 102, 100, "Taxation"});
            dt.Rows.Add(new object[] { 201, 200, "Computer"});
            dt.Rows.Add(new object[] { 103, 101, "Computer Tax"});
            dt.Rows.Add(new object[] { 202, 201, "Operating System"});
            dt.Rows.Add(new object[] { 109, 101, "Small Business Tax"});

            DataRow row = dt.AsEnumerable().Where(x => x.Field<long>("Category Id") == 202).FirstOrDefault();
            DataRow parentId = GetParent(row);
            string results = "";
            if (parentId != null)
            {
                results = string.Join(",", parentId.ItemArray);
            }
        }

        static DataRow GetParent(DataRow row)
        {
            DataRow parent = null;
            long pCategory = row.Field<long>("Parent Category Id");

            DataRow parentRow = dt.AsEnumerable().Where(x => x.Field<long>("Category Id") == pCategory).FirstOrDefault();

            if (parentRow != null)
            {
                parent = GetParent(parentRow);
            }
            else
            {
                parent = row;
            }

            return parent;
        }
    }

}

这里是测试 -1

       static DataRow GetParent(DataRow row)
        {
            DataRow parent = null;
            long pCategory = row.Field<long>("Parent Category Id");

            DataRow parentRow = dt.AsEnumerable().Where(x => x.Field<long>("Category Id") == pCategory).FirstOrDefault();
            long parentCategory = parentRow.Field<long>("Parent Category ID");

            if (parentCategory != -1)
            {
                parent = GetParent(parentRow);
            }
            else
            {
                parent = parentRow;
            }

            return parent;
        }

【讨论】:

  • 顶层根据父类id值-1决定
  • 我的 null 工作相同,因为没有 id 为 -1 的类别。
  • 我为 -1 添加了解决方案测试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-08
  • 2020-06-20
  • 2011-09-03
  • 2012-03-01
  • 2015-07-28
  • 1970-01-01
  • 2011-10-20
相关资源
最近更新 更多