I am trying to turn an IEnumerable<KeyValuePair<string, object>> into an ILookup<string, object> using the following code:
var list = new List<KeyValuePair<string, object>>()
{
new KeyValuePair<string, object>("London", null),
new KeyValuePair<string, object>("London", null),
new KeyValuePair<string, object>("London", null),
new KeyValuePair<string, object>("Sydney", null)
};
var lookup = list.ToLookup<string, object>(a => a.Key);
But the compiler is complaining with:
Instance argument: cannot convert from 'System.Collections.Generic.List>' to 'System.Collections.Generic.IEnumerable'
and
'System.Collections.Generic.List>' does not contain a definition for 'ToLookup' and the best extension method overload 'System.Linq.Enumerable.ToLookup(System.Collections.Generic.IEnumerable, System.Func)' has some invalid arguments
and
cannot convert from 'lambda expression' to 'System.Func'
What am I doing wrong with the lambda expression?
Just remove <string, object> for the types to be inferred automatically:
var lookup = list.ToLookup(a => a.Key);
As it really should be:
var lookup = list.ToLookup<KeyValuePair<string, object>, string>(a => a.Key);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With