![](/img/trans.png)
[英]get list from xml using linq based on true if there is no true then select false condition tag element in c#
[英]Get a value from a list where a condition is true using Select()
我有一個 JSON 數組,我需要從該部分的 CustomerId 屬性存在的屬性中獲取 CustomerId 值,我在下面嘗試遍歷類別並跳過其屬性樹下沒有 CustomerId 屬性的類別
var customerId = "";
foreach (var category in JObject.Parse(someData)?["categories"])
{
val = category?["sections"].FirstOrDefault()
?["areas"]?.FirstOrDefault()
?["components"]?.
?["variables"]?.FirstOrDefault()
?["properties"]
?["customerId"]?.ToString();
if (val == null)
continue;
else
{
customerId = val;
break;
}
}
問題是這看起來效率低下(可讀性較差),因為我想有一個很好的.Select
可以用來獲得相同的結果而無需 forEach 元素並檢查屬性是否為 null。
請注意,這不是我遇到的問題,這是有效的,我只想使用Select
而不是ForEach
以更具可讀性的方式執行此操作。
樣本 JSON 數據
{
"categories": [
{
"identifier": "cat1",
"sections": [
{
"identifier": "030615e9-67f9-43ca-a06e-194e7afadccb",
"properties": {},
"areas": [
{
"identifier": "1206f27b-d354-4bfa-9b5e-1fe6b4f7cc83",
"componenets": [
{
"identifier": "49e4550f-8001-4d32-b796-a7ad4423e118",
"type": "Product",
"variables": [
{
"identifier": "0d260417-fa6d-492b-85f1-dc2565bc4805",
"properties": {
"description": ""
}
}
]
}
]
}
]
}
]
},
{
"identifier": "cat2",
"sections": [
{
"identifier": "00b5bfa6-f482-47c2-bdd7-aaac273b7772",
"properties": {},
"areas": [
{
"identifier": "1ca9d051-5ec0-45af-84bc-561cd7620efa",
"componenets": [
{
"identifier": "c73b1e52-3acd-4020-8fc5-adfef7d633b4",
"type": "Customer",
"variables": [
{
"identifier": "0064e872-5c7f-4ec7-a2d6-e8b35b94bd1d",
"properties": {
"customerId": { "Text":"MainId",
"Value":"A12123"
}
}
}
]
}
]
}
]
}
]
}
]
}
另一種方法是使用.SelectTokens
庫中的 .SelectTokens (您正在使用它)。 假設parsedJson
是您解析的根 object 作為JObject
:
var customerId = parsedJson.SelectTokens("$.categories..sections..areas..componenets[?(@.type == 'Customer')]..variables..properties.customerId.Value")
.FirstOrDefault()?
.ToString();
這實際上找到了第一個條目,其中組件具有類型“Customer”和屬性“customerId”,並將該 customerId 作為字符串返回。
嚴格來說,您不需要查詢類型 ( [?(@.type == 'Customer')]
),但我覺得這就是您想要的。 沒有它,查詢將如下所示:
"$.categories..sections..areas..componenets..variables..properties.customerId.Value"
在此處的文檔中查看有關SelectTokens
的更多信息。
如果您想使用與您提出的問題相同的邏輯,您可以使用下面的代碼。
首先,您遍歷select
customerId
字段所在的所有categories
。 這將生成一個 customerId 列表,其中有些是null
,有些可能有值。
下一個SingleOrDefault
從該列表中獲取第一個確實有值的項目。 這將是您的string customerId
。
注意:當您的 json 中沒有 customerId 時, customerId
將為null
。 如果您想拋出異常而不是使用 null,您可以使用First()
而不是FirstOrDefault
。
var customerId = JObject.Parse(someData)?["categories"]
.Select(category => category?["sections"].FirstOrDefault()
?["areas"]?.FirstOrDefault()?["components"]
?["variables"]?.FirstOrDefault()
?["properties"]
?["customerId"]?.ToString())
.SingleoOrDefault(customerId => customerId != null);
我有一個優化版本:
var pJson = JObject.Parse(someData);
JToken? customerToken = pJson.SelectToken($"$..customerId");
var customerId = customerToken?.ToString();
我首先將 JSON 解析為對象列表,然后使用 Linq 獲取不是 null 的 CusomerIds 列表。
List<T> arrayItems = jsonBuidler?["categories"].ReadFromJson<List<T>>();
List<string> customerIds = arrayItems
.Where(_ => CustomerId != null)
.Select(_ => _.CustomerId);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.