kafka
kafka

Reputation: 723

LINQ query to use right key for the correct keyvaluepair

I have a list attributes, which are defined as such

class Attribute 
{
   public string Name {get; set;}
   public KeyValuePair<string, object>
}

and list of keys stored in a IList<string> listOfKeys

I would like to store all the values in a list of objects, but how do I using a linq query parse the right key, and extract the value and store them to a list<object>

What I have tried so far i something like

List<attribute> attributes;

some init stuff 

attributes.Where(x => x.Key). 

how do use my keys here?

Upvotes: 1

Views: 42

Answers (1)

StepUp
StepUp

Reputation: 38114

Try to use combination of Any:

IList<string> listOfKeys = new List<string>();
var attributes =  new List<Attribute>();            
attributes.Where(a => listOfKeys.Any(l => a.Foo.Key == l));

And Attribute class:

class Attribute
{
    public string Name { get; set; }
    public KeyValuePair<string, object>  Foo { get; set; }
}

UPDATE:

If you want get IEnumerable<object>:

var result = attributes.Where(a => listOfKeys.Any(l => a.Foo.Key == l))
    .Select(s => s.Foo.Value);

Upvotes: 1

Related Questions