Using Linq selects an item outside the list.

I have a list like

     {
        "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"

     }

And I need to find a common name for elements that have an empty string value for cholesterol.

I tried this using lambda linq expression as

var elements=items.SelectMany(c =>c.Nutrients)
    .Where(a => !string.IsNullOrEmpty(a.cholesterol))
    .ToList();

My question is how to get a common name from this condition

+4
source share
2 answers
var elements = items.Where(x => x.Nutrients.Any(a => !string.IsNullOrEmpty(a.cholesterol)))
   .Select(x => x.GeneralName)
   .ToList();

This will give you a list of all the GeneralName values, where the containing Nutrients collection contains a non-empty cholesterol value. Is this what you are looking for?


Edit:

Is there a way to get the instance_name of these elements?

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();
+5
source

The following is a modification of the source code using SelectManyto achieve the same:

var elements = items.SelectMany(i => 
                                i.nutrients.Select(n => new { n, i.CountryOfOrigin, i.GeneralName}
                              ).Where(h => !string.IsNullOrEmpty(h.n.cholesterol)
                              ).GroupBy(g => new { g.CountryOfOrigin, g.GeneralName }
                              ).Select(s => new Item {
                                                       GeneralName = s.Key.GeneralName,
                                                       CountryOfOrigin = s.Key.CountryOfOrigin,
                                                       nutrients = s.Select(v => v.n).ToList()
                                                     }
                             );

Explanation:

  • Smoothing Using Select Many
  • Empty Chloestrol.
  • GroupBy GeneralName CountryofOrigin,
  • GroupBy
  • Result
0

Source: https://habr.com/ru/post/1655651/


All Articles