none
LINQ to Entities does not recognize the method 'System.Collections.Generic.Dictionary' RRS feed

  • Question

  • Hello,

    I have the following:

    IEnumerable<JobView> jobs = _jobRepository.Where(j => j.Available == true).Select(j => new JobView() {
       Appointment = j.Appointment,
       Devices = j.Devices.ToDictionary(e => e.Serial, e => e.Kind.Name)
    });
    
    

    I get the following error:

      LINQ to Entities does not recognize the method 'System.Collections.Generic.Dictionary'

    Is there a way to overcome this?

    Thank You,

    Miguel

    Monday, November 7, 2011 2:23 PM

Answers

  • Hi Miguel;

    The issue is that this line in the query :

    Devices = j.Devices.ToDictionary(e => e.Serial, e => e.Kind.Name)

    can not be translated into a valid T-SQL statement, does not know how to handle ToDictionary, to be sent to the server to be executed. In order to do what you need you must return an anonymous type and then build the JobView object on the local machine. Something like the following:

     

    var jobs = _jobRepository.Where(j => j.Available == true)
                      .Select(j => new 
                       {
       			Appointment = j.Appointment,
       			Devices = j.Devices
    	           });
    						  
    List<JobView> jobsView = new List<JobView>();
    
    foreach( var job in jobs )
    {
    	jobsView.Add( new JobView() { Appointment = job.Appointment, 
    		Devices = job.Devices.ToDictionary(e => e.Serial, e => e.Kind.Name)});
    }						  
    
    

     

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, November 7, 2011 4:48 PM
  • Something like this should work

    IEnumerable<JobView> jobs = _jobRepository.Where(j => j.Available == true)
    .Select(j => new {
       Appointment = j.Appointment,
       Devices = j.Devices.Select(e => new { Serial = e.Serial, KindName = e.Kind.Name})
    })
    .AsEnumerable()
    .Select(j => new JobView() {
       Appointment = j.Appointment,
       Devices = j.Devices.ToDictionary(e => e.Serial, e => e.KindName)
    });
    
    As Fernandoo said the ToDictionary() cannot be translated to SQL so you first need to select all the data you need, then switch from Queryable to Enumerable and then turn the selected list of devices to a dictionary.
    ----------------------------------
    http://jendaperl.blogspot.com
    A Perl developer in the world of C#
    Tuesday, November 8, 2011 1:29 PM

All replies

  • Hi Miguel;

    The issue is that this line in the query :

    Devices = j.Devices.ToDictionary(e => e.Serial, e => e.Kind.Name)

    can not be translated into a valid T-SQL statement, does not know how to handle ToDictionary, to be sent to the server to be executed. In order to do what you need you must return an anonymous type and then build the JobView object on the local machine. Something like the following:

     

    var jobs = _jobRepository.Where(j => j.Available == true)
                      .Select(j => new 
                       {
       			Appointment = j.Appointment,
       			Devices = j.Devices
    	           });
    						  
    List<JobView> jobsView = new List<JobView>();
    
    foreach( var job in jobs )
    {
    	jobsView.Add( new JobView() { Appointment = job.Appointment, 
    		Devices = job.Devices.ToDictionary(e => e.Serial, e => e.Kind.Name)});
    }						  
    
    

     

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, November 7, 2011 4:48 PM
  • Something like this should work

    IEnumerable<JobView> jobs = _jobRepository.Where(j => j.Available == true)
    .Select(j => new {
       Appointment = j.Appointment,
       Devices = j.Devices.Select(e => new { Serial = e.Serial, KindName = e.Kind.Name})
    })
    .AsEnumerable()
    .Select(j => new JobView() {
       Appointment = j.Appointment,
       Devices = j.Devices.ToDictionary(e => e.Serial, e => e.KindName)
    });
    
    As Fernandoo said the ToDictionary() cannot be translated to SQL so you first need to select all the data you need, then switch from Queryable to Enumerable and then turn the selected list of devices to a dictionary.
    ----------------------------------
    http://jendaperl.blogspot.com
    A Perl developer in the world of C#
    Tuesday, November 8, 2011 1:29 PM