All you're missing is the type of KeyValuePair to use. You could use Jared's lambda expression version, or if you like the query expression syntax:
var toReturn = from kv in source
select new KeyValuePair<MyType, OtherType>(kv.Key,
kv.Value.First());
If you don't want to explicitly specify the type arguments, you could create your own helper class and use type inference:
// Non-generic helper class.
public static class KeyValuePair
{
public static KeyValuePair<TKey, TValue> Of(TKey key, TValue value)
{
return new KeyValuePair<TKey, TValue>(key, value);
}
}
at which point your query becomes:
var toReturn = from kv in source
select KeyValuePair.Of(kv.Key, kv.Value.First());
or
var toReturn = source.Select(kv => KeyValuePair.Of(kv.Key, kv.Value.First());
EDIT: Oops - I think I'd misread your question as well. If you want it to be of the exact same type, i.e. with the value being enumerable too, just use Take(1)
instead of First()
:
var toReturn = source.Select(kv => KeyValuePair.Of(kv.Key, kv.Value.Take(1));
(Isn't type inference lovely? :)