Hi this seems like it should work,
from something in collectionofsomestuff
select new SelectListItem(){Text = something.Name, Value = something.SomeGuid.ToString(), Selected = false};
When I try to do this it doesn't work give me error
LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.
Is there a workaround?
I ended up doing a foreach like so
this is the only way I could get it to work..it wasn't what i was hoping to do tough..
You can get the records in the db,and then turn them to a list or a array use ToList() or ToArray().Then use the object. For example(it is LINQ to Entities ):
I don't speak Linq query expressions too well, but the following should do the trick:
Create a constructor for SelectListItem that accepts your Value as a Guid and ToString it there. Now call your query like so:
Not all CLR methods can be used with Linq-to-Entities. ToString() seems to be one of them.
Take a look at CLR Method to Canonical Function Mapping.
Maybe try setting the GUID to a string variable explicitly, outside of Linq.
I had the same problem, and I ended up changing my object's definition to get around the problem. It's a complete hack, but it allows me to populate the data straight from the query:
And the query works as designed because it has something else doing the conversion for it:
It makes the object a little messier, but makes dealing with the Guid in the query so much easier.