我有一个像这样的json字符串列表
parent_id
现在我想得到与规则ID匹配的元素数。如何使用linq
答案 0 :(得分:1)
执行以下操作:
string pattern = @"<a regular expression pattern>";
Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
var num = jsonList.Count(x => rgx.Matches(JObject.Parse(x)["RuleId"]).Count > 0);
或者在一个简单的案例中:
string specifiedId = "<Id>";
var num = jsonList.Count(x => JObject.Parse(x)["RuleId"] == specifiedId);
答案 1 :(得分:0)
这是我从你的问题中理解的:在列表的元素中计算给定的id。
var ruleId = "20cf47c5-3674-4a39-8553-00348f860fe7";
var list = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("RuleId", "20cf47c5-3674-4a39-8553-00348f860fe7"),
new KeyValuePair<string, string>("RuleId", "3047c5-3674-4a39-7753-003477860fe7"),
new KeyValuePair<string, string>("RuleId", "20cf47c5-3674-4a39-8553-00348f860fe7"),
new KeyValuePair<string, string>("RuleId", "55cf47c5-3674-4a39-8553-00366f860hh7")
};
var count = list.Count(x => x.Value == ruleId);