.net 在动态方法中按字母顺序对JArray排序

jdzmm42g  于 2023-04-22  发布在  .NET
关注(0)|答案(1)|浏览(117)
"ethernet":[
{
"name":"Ethernet",
"MacAddress":"24-1C-04-11-BF-41",
"Status":"Up",
"LinkSpeed":"100 Mbps",
"MediaType":"802.3",
"AdminStatus":"Up",
"NetworkName":"Unidentified network",
"DriverInfo":"Driver Date 2015-04-05 ",
"Description":"Realtek PCIe GBE Family ",
"LinkLayerAdress":"24-1C-04-11-BF-41",
"GUID":"{B0D12A82-506B-4C8A-B5BD-3F8",
"PortNotAuthenticated":"False",
"InterfacePaused":"False",
"LowPower":"False",
"Disconnected":"False"
},
{
"name":"Ethernet 2",
"MacAddress":"70-88-6B-89-C6-FC",
"Status":"Up",
"LinkSpeed":"100 Mbps",
"MediaType":"802.3",
"AdminStatus":"Up",
"NetworkName":"Unidentified network",
"DriverInfo":"Driver Date 2015-09-17 ",
"Description":"Realtek USB GbE Family ",
"LinkLayerAdress":"70-88-6B-89-C6-FC",
"GUID":"{C2A0D6A5-0769-493A-BD4A-D3E",
"PortNotAuthenticated":"False",
"InterfacePaused":"False",
"LowPower":"False",
"Disconnected":"False"
}
]

我有这个JSON对象数组,我想按MacAddress条目对其进行排序。
我见过很多解决方案(e.g.),它们利用lambda表达式按JObject值之一对JArray进行排序,但是当我尝试构建它时,我得到了下面的错误

JArray Ethernets_sorted = new JArray(Ethernets.OrderByDescending( obj => obj["MacAddress"]));
Error   CS1977  Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type.

我假设是这种情况,因为具有此代码的方法被声明为动态的。如何将lambda表达式转换为已知类型?我对C#和.NET很陌生,抱歉问一些琐碎的问题。这是在.NET Framework 4.6.2到Visual Studio 2022中运行的

62lalag4

62lalag41#

就像你链接的例子:你必须确保编译器知道你的lambda函数的结果是什么类型。

Ethernets.OrderByDescending(obj => obj.Value<string>("MacAddress"))

应该可以

相关问题