2

Say I have a database which contains the elements such as:

{
    "elmName" : "elm1",
    "arrayOfStrings" : [ "str1", "str4", "str2"]
},
{
    "elmName" : "elm2",
    "arrayOfStrings" : ["str4", "str3"]
},
{
    "elmName" : "elm3",
    "arrayOfStrings" : [ "str1"]
},
{
    "elmName" : "elm4",
    "arrayOfStrings" : []
}

The strings in the 'arrayOfStrings' are random and unknown at search time. how can I query the count of all unique elements under 'arrayOfStrings' such as:

{
"str4" : 2,
"str2" : 1,
"str1" : 2,
"str3" : 1}

Thanks!

Avishay
  • 305
  • 1
  • 11

1 Answers1

0

You can use LINQ to flatten all the sublists into one, then group by key and count the number of occurrences:

 ListOfObjects.SelectMany(x => x.arrayOfStrings).GroupBy(x => x).Select(x => new KeyValuePair<string, int>(x.Key, x.Count()))

ElasticLinq

Michał Żołnieruk
  • 2,095
  • 12
  • 20