none
LINQ to Entities does not recognize the method 'System.Collections.Generic.Dictionary`. Please, need help. Thank you. RRS feed

  • Question

  • Hello,

    I have the following code:

       IEnumerable<JobView> _context.Jobs.Where(j => EntityFunctions.TruncateTime(j.Appointment) == appointment.Date).Select(j => new JobView() {
        Appointment = j.Appointment,
        Devices = j.Devices.Select(d => new { Key = d.Serial, Value = d.Kind.Name }).ToDictionary(k => k.Key, v => v.Value),
        Id = j.Id,
        Zone = j.Zone
       });
    


    But I keep getting the error:

    LINQ to Entities does not recognize the method 'System.Collections.Generic.Dictionary`2[System.String,System.String] ToDictionary[<>f__AnonymousType0`2,String,String](System.Collections.Generic.IEnumerable`1[<>f__AnonymousType0`2[System.String,System.String]], System.Func`2[<>f__AnonymousType0`2[System.String,System.String],System.String], System.Func`2[<>f__AnonymousType0`2[System.String,System.String],System.String])' method, and this method cannot be translated into a store expression.

     

    Could someone, please, help me how to solve this?

    Thank you,

    Miguel

    Thursday, June 23, 2011 5:48 PM

Answers

  • Try to split it into two steps, first get the data, then "convert" to Enumerable and then build the dictionary and the JobView() object:

    IEnumerable<JobView> _context.Jobs
     .Where(j => EntityFunctions.TruncateTime(j.Appointment) == appointment.Date)
     .Select(j => new {
      Appointment = j.Appointment,
      Devices = j.Devices.Select(d => new { Key = d.Serial, Value = d.Kind.Name }),
      Id = j.Id,
      Zone = j.Zone
     })
     .AsEnumerable()
     .Select(j => new JobView() {
      Appointment = j.Appointment,
      Devices = j.Devices.ToDictionary(k => k.Key, v => v.Value),
      Id = j.Id,
      Zone = j.Zone
     });
    
    


    (untested!)

     

    Jenda


    ----------------------------------
    http://jendaperl.blogspot.com
    A Perl developer in the world of C#
    Friday, June 24, 2011 12:21 PM

All replies

  • On 6/23/2011 1:48 PM, MDMoura wrote:
    > Hello,
    >
    > I have the following code:
    >
    >     IEnumerable<JobView>  _context.Jobs.Where(j =>  EntityFunctions.TruncateTime(j.Appointment) == appointment.Date).Select(j =>  new  JobView() {
    >      Appointment = j.Appointment,
    >      Devices = j.Devices.Select(d =>  new  { Key = d.Serial, Value = d.Kind.Name }).ToDictionary(k =>  k.Key, v =>  v.Value),
    >      Id = j.Id,
    >      Zone = j.Zone
    >     });
    >
    >
    > But I keep getting the error:
    >
    > LINQ to Entities does not recognize the method
    > 'System.Collections.Generic.Dictionary`2[System.String,System.String]
    > ToDictionary[<>f__AnonymousType0`2,String,String](System.Collections.Generic.IEnumerable`1[<>f__AnonymousType0`2[System.String,System.String]],
    > System.Func`2[<>f__AnonymousType0`2[System.String,System.String],System.String],
    > System.Func`2[<>f__AnonymousType0`2[System.String,System.String],System.String])'
    > method, and this method cannot be translated into a store expression.
    >
    > Could someone, please, help me how to solve this?
    >
     
    The Dictionary stuff cannot be translated to T-SQL (the store
    expression). You need to find another way.
     
    Thursday, June 23, 2011 6:12 PM
  • I think this might be possible with a sub query (sub lambda expression) or something.

    But I am not able to solve it ... I tried many options and searched for one.

    Does anyone has any idea how to solve this?

    Thank You,

    Miguel

    Thursday, June 23, 2011 6:16 PM
  • Is there an alternative C# native data format so I can store this data?

    I also tried an array but no luck.

    Thursday, June 23, 2011 6:19 PM
  • Try to split it into two steps, first get the data, then "convert" to Enumerable and then build the dictionary and the JobView() object:

    IEnumerable<JobView> _context.Jobs
     .Where(j => EntityFunctions.TruncateTime(j.Appointment) == appointment.Date)
     .Select(j => new {
      Appointment = j.Appointment,
      Devices = j.Devices.Select(d => new { Key = d.Serial, Value = d.Kind.Name }),
      Id = j.Id,
      Zone = j.Zone
     })
     .AsEnumerable()
     .Select(j => new JobView() {
      Appointment = j.Appointment,
      Devices = j.Devices.ToDictionary(k => k.Key, v => v.Value),
      Id = j.Id,
      Zone = j.Zone
     });
    
    


    (untested!)

     

    Jenda


    ----------------------------------
    http://jendaperl.blogspot.com
    A Perl developer in the world of C#
    Friday, June 24, 2011 12:21 PM