I have this code
var contacts = dr.mktDoctorContacts
.GroupBy(x => x.ContactType)
.Select(zb => new
{
Key = zb.Key,
GroupWiseContacts = zb.Select(x => x.Contact).ToList()
})
.ToDictionary<string,List<string>>(y => y.Key, y => y.GroupWiseContacts)
I don’t know what is wrong with this code.
Compile time error msg says:System.Generic.IEnumerable does not contain definition of and best extension method overloads has some invalid arguments. i can see only two overloads of ToDictionary Method in my visual studio tooltip sort of documentation whereas i have come across more than two overloads of ToDictionary on the web
Edit Here is exact Error message at compile time
Error 13 ‘
System.Collections.Generic.IEnumerable<AnonymousType#1>‘
does not contain a definition for
‘ToDictionary‘ and the best extension
method overload
‘System.Linq.Enumerable.ToDictionary<TSource,TKey>(System.Collections.Generic.IEnumerable<TSource>,‘
System.Func<TSource,TKey>,
System.Collections.Generic.IEqualityComparer<TKey>)
has some invalid arguments
The compiler message makes the error clear: There is no method
ToDictionarywhich can accept the arguments and types specified.The mistake here is in specifying the type arguments on
ToDictionary. The MSDN documentation defines the extension method asToDictionary<TKey, TSource>. The source in your example isIEnumerable<AnonymousType#1>, but you have specified a type ofList<string>.To correct the error, omit the type arguments; the compiler will infer the correct types. You can additionally combine the
SelectandToDictionarytransformations into a single statement: