C#新手-对象列表,用于构建简单的列表对象。以此类推

C#新手-对象列表,用于构建简单的列表对象。以此类推,c#,C#,嘿,大家好,我的精神被卡住了 我有一个从Web API检索到的对象列表,它有三个值,包括父ID、字符串值和行ID 即: 类别ID名称父ID 1工具0 2锤1 3螺丝刀1 4菲利普斯3 5标准3 6#2 4 7.3 8#15 7 等等 这需要放入一个简单的列表对象中,该对象由一个ParentID和一个直接类别名称和父id的连接字符串组成 类别ID FullCategoryName 0工具 2个工具/锤子 3工具/螺丝刀 4工具/螺丝刀/菲利普斯 5工具/螺丝刀/标准 6工具/螺丝刀/菲利普

嘿,大家好,我的精神被卡住了

我有一个从Web API检索到的对象列表,它有三个值,包括父ID、字符串值和行ID

即:

类别ID名称父ID

1工具0

2锤1

3螺丝刀1

4菲利普斯3

5标准3

6#2 4

7.3

8#15 7

等等

这需要放入一个简单的列表对象中,该对象由一个ParentID和一个直接类别名称和父id的连接字符串组成

类别ID FullCategoryName

0工具

2个工具/锤子

3工具/螺丝刀

4工具/螺丝刀/菲利普斯

5工具/螺丝刀/标准

6工具/螺丝刀/菲利普斯/#2

7工具/螺丝刀/Torx

8工具/螺丝刀/梅花/#15

我希望您能够看到,我需要categoryID和基于parentID的完整路径,并使用斜杠

API调用类

public class Categories_Web
{
    public string CategoryName { get; set; }
    public int CategoryParent { get; set; }
    public int CategoryID { get; set; }
}
具有串联名称的简化类

public class WebCategoriesSimple
{
    public int CategoryID { get; set; }
    public string CategoryName { get; set; }
}

我希望这是有意义的,谢谢你的帮助

这是我认为LINQ最适合解决的问题之一。我在理解你的数据格式方面有点困难。但是,可以使用LINQ的投影将原始列表的结果投影到包含基于类定义的新对象或新匿名类的新列表中

使用LINQ投影到一个新的匿名类中,看起来如下所示。请记住,我们不知道您的对象名称,因为您没有提供这些信息,所以您可能需要从我的示例中推断一些内容

(请注意,我重新阅读了您的帖子,并且我认为我理解了您的原始对象可能被调用的内容,因此该帖子已被编辑为使用您的类名。)

字符串是使用字符串插值创建的,这在C#中是新的。如果您的C#版本不支持此功能,您可以使用string.Format,如下所示:

string.Format("Tools/{0}/#{1}", s.CategoryName, s.CategoryParent);
foreach(var item in newListOfStuff)
{
    Console.WriteLine(item.CategoryID);
    Console.WriteLine(item.CategoryName);
}
使用该新列表,您可以循环浏览它并使用如下属性:

string.Format("Tools/{0}/#{1}", s.CategoryName, s.CategoryParent);
foreach(var item in newListOfStuff)
{
    Console.WriteLine(item.CategoryID);
    Console.WriteLine(item.CategoryName);
}
以下是一个类似的参考答案:

你有一个层次结构,每当你有这个层次时,你可以考虑递归——一种调用它自己的方法。您并不总是希望使用递归,但对于可管理的大小列表或尾部调用优化的递归方法,它是一个强大的工具

以下是输出以下内容的演示代码:

Category: 1, Hierarchy: Tools
Category: 2, Hierarchy: Tools/Hammer
Category: 3, Hierarchy: Tools/ScrewDriver
Category: 4, Hierarchy: Tools/ScrewDriver/Phillips
Category: 5, Hierarchy: Tools/ScrewDriver/Standard
Category: 6, Hierarchy: Tools/ScrewDriver/Phillips/#2
Category: 7, Hierarchy: Tools/ScrewDriver/Torx
Category: 8, Hierarchy: Tools/ScrewDriver/Torx/#15
(注意,我认为“0,Tools”的示例输出不正确)

此程序创建
ProductDef
的硬编码列表。你的可能来自数据库或其他什么

然后,它创建一个
ProductHierarchy
的空列表,该列表将在递归操作运行时填充

然后,它通过第一次调用
Build()
开始递归。在该方法中,当传入的项在层次结构中具有父级时,它将调用自身

当不再有父项时,它会将该项添加到
ProductHierarchy
的列表中

void Main()
{
    List<ProductDef> pdefs = new List<UserQuery.ProductDef>{
        new ProductDef{Category = 1, Product = "Tools",  ParentCategory = 0},
        new ProductDef{Category = 2, Product = "Hammer",  ParentCategory = 1},
        new ProductDef{Category = 3, Product = "ScrewDriver",  ParentCategory = 1},
        new ProductDef{Category = 4, Product = "Phillips",  ParentCategory = 3},
        new ProductDef{Category = 5, Product = "Standard",  ParentCategory = 3},
        new ProductDef{Category = 6, Product = "#2",  ParentCategory = 4},
        new ProductDef{Category = 7, Product = "Torx",  ParentCategory = 3},
        new ProductDef{Category = 8, Product = "#15",  ParentCategory = 7}
    };

    //This will get filled as we go
    List<ProductHierarchy> phlist = new List<UserQuery.ProductHierarchy>();

    //kick off the recursion
    foreach (var element in pdefs)
    {
        Build(element, pdefs, element.Product, element.Category, phlist);
    }

    //do stuff with your list
    foreach (ProductHierarchy ph in phlist)
    {
        Console.WriteLine(ph.ToString());
    }
}

class ProductDef
{
    public int Category { get; set; }
    public string Product { get; set; }
    public int ParentCategory { get; set; }
}

class ProductHierarchy
{
    public int Category { get; set; }
    public string Hierarchy { get; set; }
    public override string ToString()
    {
        return $"Category: {Category}, Hierarchy: {Hierarchy}";
    }
}

void Build(ProductDef def, List<ProductDef> lst, string fullname, int cat, List<ProductHierarchy> phlist)
{
    string fullprodname = fullname;

    //obtain the parent category of product
    var parent = lst.FirstOrDefault(l => l.Category == def.ParentCategory);
    if (parent != null)
    {
        fullprodname = $"{parent.Product}/{fullprodname}";
        //recurse the call to see if the parent has any parents
        Build(parent, lst, fullprodname, cat, phlist);
    }
    else
    {
        //No further parents found, add it to a list
        phlist.Add( new ProductHierarchy { Category = cat, Hierarchy = fullprodname }); 
    }
}
void Main()
{
List pdefs=新列表{
新产品定义{Category=1,Product=“Tools”,ParentCategory=0},
新产品定义{Category=2,Product=“Hammer”,ParentCategory=1},
新产品定义{Category=3,Product=“螺丝刀”,ParentCategory=1},
新产品定义{Category=4,Product=“Phillips”,ParentCategory=3},
新产品定义{Category=5,Product=“Standard”,ParentCategory=3},
新产品定义{Category=6,Product=“#2”,ParentCategory=4},
新产品定义{Category=7,Product=“Torx”,ParentCategory=3},
新产品定义{Category=8,Product=“#15”,ParentCategory=7}
};
//我们走的时候,这个会被填满的
List phlist=新列表();
//开始递归
foreach(pdefs中的var元素)
{
构建(元素、pdefs、元素、产品、元素、类别、phlist);
}
//用你的清单做些事情
foreach(phlist中的产品层次结构ph)
{
Console.WriteLine(ph.ToString());
}
}
类ProductDef
{
公共int类{get;set;}
公共字符串乘积{get;set;}
public int ParentCategory{get;set;}
}
类ProductHierarchy
{
公共int类{get;set;}
公共字符串层次结构{get;set;}
公共重写字符串ToString()
{
返回$“Category:{Category},Hierarchy:{Hierarchy}”;
}
}
无效生成(ProductDef、List lst、字符串fullname、int cat、List phlist)
{
字符串fullprodname=fullname;
//获取产品的父类别
var parent=lst.FirstOrDefault(l=>l.Category==def.ParentCategory);
如果(父项!=null)
{
fullprodname=$“{parent.Product}/{fullprodname}”;
//递归调用以查看父级是否有任何父级
构建(父级、lst、fullprodname、cat、phlist);
}
其他的
{
//找不到其他家长,请将其添加到列表中
添加(新产品层次结构{Category=cat,层次结构=fullprodname});
}
}

欢迎访问stackoverflow.com。请花些时间阅读,特别是命名和。也请和。最后,请学习如何创建一个。也请学习。并确保选择了正确的标记。@fullbugg您应该花一些时间学习用于设置问题格式的标记。它很难阅读,可能会让能帮助你的人感到厌烦。很抱歉,页面的格式对我来说是正常的。清理列表和类。你看到了什么?谢谢你的回答…这很接近,我同意,从我对LINQ所知的一点来看,这是一条路要走!!!在层次关系的SQL中,可以将ChildID连接回ParentID,在这种情况下,父对象是ParentCategoryID,而ChildID是CategoryID。然后了解它们之间的关系