How to filter a ListDictionarystring, object?

4

I have a list variable that is a List >.

She has 108 items.

The Matrix [0] list looks like this:

What I wanted was for the variable ListMatrix to come only with the items in the Matrix list whose COD_OCUPACAO = 1 & COD_NIVEL = 1

I have tried in several ways, eg:

var listaMatrizFiltrada = listaMatriz.GroupBy(i => i.ElementAt(0).Value == 1 && i.ElementAt(4).Value == 1).Where(group => @group.Any()).Select(g => g.First()).ToDictionary(kvp => kvp.Keys, kvp => kvp.Values);

But it did not filter correctly. This code I found on the internet, but I still do not understand exactly how each section (GroupBy, Where, etc.) works.

    
asked by anonymous 15.10.2015 / 16:44

1 answer

4

You can simplify this a lot more:

var listaMatrizFiltrada = listaMatriz
                          .Where(item => item["COD_OCUPACAO"] == 1 &&         
                                           item["COD_NIVEL"] == 1)
                          .ToList();

As ListaMatriz is a list of dictionaries, item will be Dictionary<string, object> , so I can use indexes like item["COD_OCUPACAO"] and item["COD_NIVEL"] .

I'm assuming this is an extraction of a database, and that all dictionaries in the list have the same indexes.

    
15.10.2015 / 17:08