Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using a Lambda Expression Over a Dictionary<string, List<object>> in C#

I have a dictionary that looks like this:

public Dictionary<string, List<EquipmentRow>> TableRows { get; set; }

EquipmentRow class:

public class EquipmentRow
{
    public string Model { get; set; }
    public bool New { get; set; }
}

I want to create/filter a structurally same Dictionary as the previous one which only contains a list of items in which property New is equal to true.
How to achieve that by using a Lambda Expression?
For example:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

var newLocationDevices = locationDevices.Where(x => x.Value.Where()) etc.

>Solution :

You can do it simply using this code:

var newLocationDevices = locationDevices
    .ToDictionary(
        o => o.Key,
        o => o.Value.Where(i => i.New).ToList()
    );
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading