Respondida Entityframework query error on compilation.

  • martes, 10 de abril de 2012 17:33
     
      Tiene código

    I have a query:

                   AmazonMWSTestEntities amazonMWSDb = new AmazonMWSTestEntities();
                   return from f in amazonMWSDb.FeedSubmissions
                          select f.ID.ToString();

    This is in a function that is returning IEnumerable<string> and when I try to compile this I get the error:

    Cannot convert lambda expression to type 'string' because it is not a delegate type

    But in other places in the project I have a similar query:

                    AmazonMWSTestEntities amazonMWSDb = new AmazonMWSTestEntities();
                    return from r in amazonMWSDb.Reports where !r.Acknowledged select r.ID.ToString();

    that yields no such error. What am I doing wrong?

    I tried surrounding the Guid that I am converting to a string with a parenthesis as the error seemed to indicate that the "ToString" was being applied to the lambda expression but this didn't help and result in the same error.

    Thank you.


    Kevin Burton


    • Editado KevinBurton martes, 10 de abril de 2012 17:36 Update
    •  

Todas las respuestas

  • miércoles, 11 de abril de 2012 2:22
    Moderador
     
      Tiene código

    Hi Kevin,

    I think you can try modify your code as following:

    AmazonMWSTestEntities amazonMWSDb = new AmazonMWSTestEntities();
                   return (from f in amazonMWSDb.FeedSubmissions
                          select f.ID).ToList().Select(t=>t.ToString()); 
    

    For your second query, amazonMWSDB.Reports is IEnmerable type, so you can use ToString() method on it.

    AmazonMWSTestEntities amazonMWSDb = new AmazonMWSTestEntities();
                   return (from f in amazonMWSDb.FeedSubmissions.ToList()
                          select f.ID.ToString());

    This query should work too, but it will query all the records in database, the performance is bad.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

  • miércoles, 11 de abril de 2012 13:38
     
      Tiene código

    I will try your solution but I will have to choke it up to something I don't understand if it works. You see when I create the database context from the database it creates the same property type for both of the tables that I wanted to include. In other words the properties Reports and FeedSubmissins return the same type, namely ObjectContext<T>. In the case of Reports the 'T' is a Report type and FeedSumissions it is a FeedSubmission type. If the query was something like:

    from r in amazonMWSDb.Reports where !r.Acknowledged select r.ID

    then I would get an IEnumerable<Guid>. So why if I change the query to

    from r in amazonMWSDb.Reports where !r.Acknowledged select r.ID.ToString()

    can't I expect to get an IEnumerable<string>?


    Kevin Burton

  • jueves, 12 de abril de 2012 9:10
    Moderador
     
     Respondida

    Hi Kenvin,

    This the difference between IQueryable and IEnmerable:

    http://social.msdn.microsoft.com/Forums/nl-NL/adodotnetentityframework/thread/35b74da4-3dd0-4c96-83df-17bc496e4b8e

    You can use SQL Profiler to watch the "T-SQLs", Entity Framework provider couldn't recognise(Translate) the "ToString()"method, so your exception is here. The way to convert the type is make the collection to be IEnumerable<> first, then convert it in memory.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marcado como respuesta KevinBurton viernes, 27 de abril de 2012 19:01
    •