c# .net无限递归获取分类,传统for无限递归和 linq无限递归获取分类
程序员文章站
2022-03-26 21:19:34
c# .net无限递归获取分类,传统for无限递归和 linq无限递归获取分类...
一、传统for无限递归获取分类
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DiGui
{
class Program
{
/// <summary>
/// 缓存数据
/// </summary>
private static List<TypeModel> typeList = null;
/// <summary>
/// 递归后的数据
/// </summary>
static List<TypeModel> RecursiveTypeList = new List<TypeModel>();
/// <summary>
/// 【递归深度】
/// 如果是2级分类
/// 一级分类是“手机”
/// 二级分类是“小米手机”
/// 递归第一次是“手机”深度是0
/// 递归第二次是“小米手机”深度是1
/// 以此类推
/// </summary>
static int RecursiveIndex = 0;
static void Main(string[] args)
{
RecursiveEXE();
Console.Read();
}
#region ////////递归给分类重新排序////////
/// <summary>
/// 递归给分类重新排序
/// </summary>
private static void RecursiveEXE()
{
typeList = GetList();
string y = "";
foreach (var type in typeList)
{
y = y + $"{type.ID}+{ type.Name} \n";
}
Console.WriteLine("源数据:");
Console.WriteLine(y);
Recursive(0);
string s = "";
foreach (var type in RecursiveTypeList)
{
s = s + type.sign+ type.Name + "\n";
}
Console.WriteLine("【递归】后的数据:");
Console.WriteLine($"{s}");
}
/// <summary>
/// 递归方法
/// </summary>
/// <param name="id"></param>
private static void Recursive(int id)
{
int a = 0;
for (int i = 0; i < typeList.Count; i++)
{
TypeModel typeModel = typeList[i];
if (typeModel.BaseID == id)
{
a = a+1;
TypeModel Type = new TypeModel();
Type.ID = typeModel.ID;
Type.Name = typeModel.Name;
Type.BaseID = typeModel.BaseID;
if (a == 1 && typeModel.BaseID != 0)
{
RecursiveIndex = RecursiveIndex + 1;
}
string sign = "";
for (int j = 0; j < RecursiveIndex; j++)
{
sign = sign + "-";
}
if (typeModel.BaseID == 0)
{
RecursiveIndex = 0;
sign = "";
}
Type.sign = sign;
RecursiveTypeList.Add(Type);
//这里需要声明一个临时变量存储当前递归深度,让这个变量去等待递归;否则,将会覆盖递归深度,造成符号数据错误,也就是 sign变量
int temporary = RecursiveIndex;
Recursive(typeModel.ID);
//当前递归结束后把临时存储的递归深度,赋值给当前深度
RecursiveIndex = temporary;
}
}
}
private static List<TypeModel> GetList()
{
List<TypeModel> typeList = new List<TypeModel>();
{
TypeModel typeModel = new TypeModel()
{
ID = 1,
Name = "手机",
BaseID = 0
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 2,
Name = "电脑",
BaseID = 0
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 3,
Name = "小米手机",
BaseID = 1
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 4,
Name = "小米电脑",
BaseID = 2
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 5,
Name = "华为手机",
BaseID = 1
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 6,
Name = "三星手机",
BaseID = 1
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 7,
Name = "联想电脑",
BaseID = 2
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 8,
Name = "戴尔电脑",
BaseID = 2
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 9,
Name = "小米5手机",
BaseID = 3
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 10,
Name = "小米游戏电脑",
BaseID = 4
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 11,
Name = "小米5手机壳",
BaseID = 9
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 12,
Name = "小米游戏电脑壳",
BaseID = 10
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 13,
Name = "小米5手机壳【白色】",
BaseID = 11
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 14,
Name = "小米游戏电脑壳【亮光黑】",
BaseID = 12
};
typeList.Add(typeModel);
}
return typeList;
}
#endregion
}
public class TypeModel
{
public int ID { get; set; }
public string Name { get; set; }
public Nullable<int> BaseID { get; set; }
public string sign { get; set; }
}
}
运行结果:
二、linq无限递归获取分类
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DiGui3
{
class Program
{
/// <summary>
/// 缓存数据
/// </summary>
static List<TypeModel> typeList = GetList();
/// <summary>
/// 递归后的数据
/// </summary>
static List<TypeModel> RecursiveTypeList =new List<TypeModel>();
/// <summary>
/// 【递归深度】
/// 如果是2级分类
/// 一级分类是“手机”
/// 二级分类是“小米手机”
/// 递归第一次是“手机”深度是0
/// 递归第二次是“小米手机”深度是1
/// 以此类推
/// </summary>
static int RecursiveIndex = 0;
static void Main(string[] args)
{
StringBuilder a = new StringBuilder();
foreach (TypeModel t in typeList)
{
a.Append($"{t.ID}{t.sign}{t.Name}\n");
}
Console.WriteLine("源数据:");
Console.WriteLine(a);
//递归后的数据
Recursive();
StringBuilder b = new StringBuilder();
foreach (TypeModel t in RecursiveTypeList)
{
b.Append($"{t.sign}{t.Name}\n");
}
Console.WriteLine(RecursiveIndex);
Console.WriteLine("递归后的数据:");
Console.WriteLine(b);
Console.Read();
}
/// <summary>
/// 递归方法
/// </summary>
/// <param name="id"></param>
private static void Recursive(int id=0)
{
string sign = "";//符号
List<TypeModel> types = typeList.Where(type => type.BaseID == id).ToList();
if (types.Count > 0)
{
if (types[0].BaseID != 0)
{
RecursiveIndex++;
}
}
for (int i = 0; i < RecursiveIndex; i++)
{
sign += "-";
}
foreach (TypeModel type in types)
{
type.sign = sign;
RecursiveTypeList.Add(type);
if (type.BaseID == 0)
{
RecursiveIndex = 0;
sign = "";
}
//这里需要声明一个临时变量存储当前递归深度,让这个变量去等待递归;否则,将会覆盖递归深度,造成符号数据错误,也就是 sign变量
int i = RecursiveIndex;
Recursive(type.ID);
//当前递归结束后把临时存储的递归深度,赋值给当前深度
RecursiveIndex = i;
}
}
#region /////准备数据////
private static List<TypeModel> GetList()
{
List<TypeModel> typeList = new List<TypeModel>();
{
TypeModel typeModel = new TypeModel()
{
ID = 1,
Name = "手机",
BaseID = 0
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 2,
Name = "电脑",
BaseID = 0
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 3,
Name = "小米手机",
BaseID = 1
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 4,
Name = "小米电脑",
BaseID = 2
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 5,
Name = "小米11手机",
BaseID = 3
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 6,
Name = "小米笔记本电脑",
BaseID = 4
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 7,
Name = "小米11的手机壳",
BaseID = 5
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 8,
Name = "小米笔记本plus电脑",
BaseID = 6
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 9,
Name = "小米11的手机壳【白色】",
BaseID = 7
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 10,
Name = "小米笔记本plus的电脑外壳",
BaseID = 8
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 11,
Name = "华为手机",
BaseID = 1
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 12,
Name = "华为电脑",
BaseID = 2
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 13,
Name = "华为10手机",
BaseID = 11
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 14,
Name = "华为10电脑",
BaseID = 12
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 15,
Name = "华为10手机壳",
BaseID = 13
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 16,
Name = "华为10电脑壳",
BaseID = 14
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 17,
Name = "华为10手机壳【黑色】",
BaseID = 15
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 18,
Name = "华为10电脑壳【银灰色】",
BaseID = 16
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 19,
Name = "空调",
BaseID = 0
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 20,
Name = "格力空调",
BaseID = 19
};
typeList.Add(typeModel);
}
{
TypeModel typeModel = new TypeModel()
{
ID = 21,
Name = "美的空调",
BaseID = 19
};
typeList.Add(typeModel);
}
return typeList;
}
#endregion
}
public class TypeModel
{
public int ID { get; set; }
public string Name { get; set; }
public Nullable<int> BaseID { get; set; }
/// <summary>
/// 符号
/// </summary>
public string sign { get; set; }
}
}
运行结果:
本文地址:https://blog.csdn.net/cplvfx/article/details/107588359