Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq ToDictionary Not Defined?

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

like image 408
Muhammad Adeel Zahid Avatar asked Feb 24 '11 12:02

Muhammad Adeel Zahid


People also ask

What is ToDictionary in linq C#?

In LINQ, ToDictionary() Method is used to convert the items of list/collection(IEnumerable<T>) to new dictionary object (Dictionary<TKey,TValue>) and it will optimize the list/collection items by required values only.

What is are the extension methods that puts elements into Dictionary based on a key selector function?

ToDictionary<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>) Creates a Dictionary<TKey,TValue> from an IEnumerable<T> according to a specified key selector function.


3 Answers

The compiler message makes the error clear: There is no method ToDictionary which 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 as ToDictionary<TKey, TSource>. The source in your example is IEnumerable<AnonymousType#1>, but you have specified a type of List<string>.

To correct the error, omit the type arguments; the compiler will infer the correct types. You can additionally combine the Select and ToDictionary transformations into a single statement:

var contacts = dr.mktDoctorContacts
    .GroupBy(x => x.ContactType)
    .ToDictionary(
        y => y.Key, 
        y => y.Select(x => x.Contact).ToList());
like image 117
Paul Turner Avatar answered Oct 23 '22 10:10

Paul Turner


Rewrote your code (and added .AsEnumerable()):

var dictionary = dr.mktDoctorContacts
    .GroupBy(x => x.ContactType)
    .AsEnumerable()
    .ToDictionary(
        i => i.Key, 
        i => i.Select(x => x.Contact).ToList()
    )
);
like image 29
Oleks Avatar answered Oct 23 '22 10:10

Oleks


Don't run that group operation in the database, it'll cause the elements of each group to be fetched in separate roundtrips.

ILookup<string, string> contacts = dr.mktDoctorContacts
  .ToLookup<Contact, string, string>(x => x.ContactType, x => x.Contact);
like image 25
Amy B Avatar answered Oct 23 '22 12:10

Amy B