Tuesday, April 10, 2012 5:33 PM
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.
- Edited by KevinBurton Tuesday, April 10, 2012 5:36 PM Update
Wednesday, April 11, 2012 2:22 AMModerator
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.
Wednesday, April 11, 2012 1:38 PM
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>?
Thursday, April 12, 2012 9:10 AMModerator
This the difference between IQueryable and IEnmerable:
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.
- Marked As Answer by KevinBurton Friday, April 27, 2012 7:01 PM