c# - 订购嵌套内部集合的 C# 最佳实践方法
问题描述
我正在以更好的方式寻找我的收藏品的订单。我有 json 对象,如下所示,
[{
"Type": "A",
"Bs": [{
"Type": "B",
"Cs": [{
"Type": "C",
"Ds": [{
"Type": "D",
"Es": [{
"Type": "E",
"Total": 10
},
{
"Type": "E",
"Total": 20
},
{
"Type": "E",
"Total": 1
}]
},
{
"Type": "D",
"Es": [{
"Type": "E",
"Total": 100
},
{
"Type": "E",
"Total": 50
},
{
"Type": "E",
"Total": 10
}]
}]
}]
},
{
"Type": "B",
"Cs": null
}]
}]
我想以更好的方式按内部集合( E模型)订购。我已经在SortByTotal方法中实现了我想要的,但我想改进它。这是我的解决方案。
private static void SortByTotal(List<A> list)
{
foreach (var a in list)
{
if (a.Bs == null) continue;
foreach (var b in a.Bs)
{
if (b.Cs == null) continue;
foreach (var c in b.Cs)
{
if (c.Ds == null) continue;
foreach (var d in c.Ds)
{
if (d.Es == null) continue;
d.Es = d.Es.OrderBy(x => x.Total).ToList();
}
}
}
}
}
这是我所有带有模型类和示例对象的代码
注意:基本上需要改进SortByTotal方法
class Program
{
static void Main(string[] args)
{
var list = new List<A>
{ new A { Bs = new List<B> { new B { Cs = new List<C> { new C { Ds = new List<D> {
new D {
Es = new List<E> { new E {Total = 10}, new E {Total = 20}, new E {Total = 1} }
},
new D {
Es = new List<E> { new E {Total = 100}, new E {Total = 50}, new E {Total = 10} }
}
} } } }, new B() } } };
Console.WriteLine("before sort");
var beforeSortList = list;
SortByTotal(list);
Console.WriteLine("after sort");
var afterSortList = list;
Console.ReadKey();
}
private static void SortByTotal(List<A> list)
{
foreach (var a in list)
{
if (a.Bs == null) continue;
foreach (var b in a.Bs)
{
if (b.Cs == null) continue;
foreach (var c in b.Cs)
{
if (c.Ds == null) continue;
foreach (var d in c.Ds)
{
if (d.Es == null) continue;
d.Es = d.Es.OrderBy(x => x.Total).ToList();
}
}
}
}
}
}
class A
{
public string Type { get; set; } = "A";
public List<B> Bs { get; set; }
}
class B
{
public string Type { get; set; } = "B";
public List<C> Cs { get; set; }
}
class C
{
public string Type { get; set; } = "C";
public List<D> Ds { get; set; }
}
class D
{
public string Type { get; set; } = "D";
public List<E> Es { get; set; }
}
class E
{
public string Type { get; set; } = "E";
public int Total { get; set; }
}
解决方案
你可以用你Sorting
的SelectTokens
方法来实现JToken
。
1)将您的 json 解析为JToken
.
2)Es
在整个JToken
.
3) 选择每个 json 对象作为JObject
.
Jobject
4) 并用 对所有s 进行排序OrderBy
。
5)最后打印你的结果。
class Program
{
static void Main(string[] args)
{
string str = File.ReadAllText(@"Path to your json file");
//Step 1
JToken jToken = JToken.Parse(str);
//Step 2 to 4
var result = jToken.SelectTokens("..Es").Select(x => x.Select(y => (JObject)y).OrderBy(z => z["Total"])).ToList();
//Step 5
foreach (var i in result)
{
foreach (var j in i)
Console.WriteLine($"Type: {j["Type"]} \t Total: {j["Total"]}");
Console.WriteLine();
}
Console.ReadLine();
}
}
输出:
推荐阅读
- sql - 如果表包含 STRUCT 类型,则 BigQuery 中两个表的通用比较方法
- swiftui - 将来自 url 的 Image() 放入 List 内 Button 内的 HStack 使其变黑
- android - 如何修复Android项目上的链接错误
- python - Array Division Linear Least lstsq 给出错误的结果
- c++ - 纯抽象类的虚拟析构函数
- powershell - 无法使用 Send-MailMessage 发送附件
- c - 马里奥问题集 - 为什么不打印哈希?
- sql-server - IntegrationServicesCatalog 不允许创建 SSISDB
- apache-kafka - Kafka kstream 比较来自两个不同主题的两个值
- ios - 可以在 macOS CLI 项目中使用 ARKit 类吗?