我有一个像
这样的清单 {
"Nutrients": [{
"vitamin": "vitamin C",
"potassium": "195mg",
"sodium": "2mg",
"cholesterol": "",
"display_name": "Apple"
}, {
"vitamin": "vitamin B",
"potassium": "176mg",
"sodium": "2mg",
"cholesterol": "",
"display_name": "Grape"
}],
"General_name": "Fruits",
"country_of_origin": "France"
}
{
"Nutrients": [{
"vitamin": "vitamin B",
"potassium": "196mg",
"sodium": "115mg",
"cholesterol": "123mg",
"display_name": "Chicken"
}, {
"vitamin": "vitamin B",
"potassium": "360mg",
"sodium": "53mg",
"cholesterol": "68mg",
"display_name": "Chicken"
}],
"General_name": "Meat",
"country_of_origin": "Denmark"
}
我需要找到具有胆固醇空字符串值的项目的General_name。
我尝试使用linq lambda表达式
var elements=items.SelectMany(c =>c.Nutrients)
.Where(a => !string.IsNullOrEmpty(a.cholesterol))
.ToList();
我的问题是如何从这个条件中获得通用名称
答案 0 :(得分:5)
var elements = items.Where(x => x.Nutrients.Any(a => !string.IsNullOrEmpty(a.cholesterol)))
.Select(x => x.GeneralName)
.ToList();
这将为您提供所有GeneralName值的列表,其中包含Nutrients集合包含胆固醇的非空值。那是你在找什么?
编辑:
有没有办法可以获得这些项目的display_name?
var elements = items.Where(x => x.Nutrients.Any(a => !string.IsNullOrEmpty(a.Cholesterol)))
.Select(x => new {x.GeneralName, DisplayNames = x.Nutrients.Where(a => !string.IsNullOrEmpty(a.Cholesterol)).Select(y => y.DisplayName).ToList()})
.ToList();