-7

I have the following code:

public class A 
{
   string Name {get; set};
   string anotherProperty {get; set};
   Dictionary<string, string> Attributes {get; set};
}
List<A> listA = new List<A>();
List<string> Filters = new List<string>();

Now, what I want to do is select all DISTINCT by name elements from listA where the dictionary Attributes key equals any value from the Filters list.

zav
  • 55
  • 1
  • 8
  • Any effort so far? – Izzy Apr 23 '18 at 11:18
  • 2
    Possible duplicate of [Find items from a list which exist in another list](https://stackoverflow.com/questions/15769673/find-items-from-a-list-which-exist-in-another-list) – BugFinder Apr 23 '18 at 11:33

1 Answers1

1

Now, what I want to do is select all DISTINCT by name elements from listA where the dictionary Attributes key equals any value from the Filters list.

To define a distinct by property you could use define a group by name and take the first one. To define a filter, you could use Keys property from the dictionary to search any key on the filters list.

var result = listA.Where(x => x.Attributes.Keys.Any(a => Filters.Contains(a)))
                  .GroupBy(x => x.Name)
                  .Select(x => x.First())
                  .ToList();
Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194