Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ Guid toString()

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?

like image 713
Sevki Avatar asked Mar 25 '10 01:03

Sevki


2 Answers

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 ):

var list = collectionofsomestuff.select(c => c).ToList();
from something in list
select new SelectListItem(){Text = something.Name, Value = something.SomeGuid.ToString(), Selected = false}; 
like image 194
gxl Avatar answered Sep 26 '22 07:09

gxl


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.

string myGuid = SomeGuid.ToString();

from something in collectionofsomestuff       
select new SelectListItem(){Text = Name, Value = myGuid, Selected = false};
like image 29
Mike Avatar answered Sep 24 '22 07:09

Mike