views:

287

answers:

4

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?

+1  A: 

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};
Mike
+2  A: 

I don't speak Linq query expressions too well, but the following should do the trick:

collectionofsomestuff //here it's LinqToEntities
    .Select(something=>new{something.Name,something.SomeGuid})
    .ToArray() //From here on it's LinqToObjects
    .Select(s=>new SelectListItem()
        {
            Text = s.Name, 
            Value = s.SomeGuid.ToString(), 
            Selected = false
        })
spender
sorry same error...
Sevki
A: 

I ended up doing a foreach like so

           List<SelectListItem> list  = new List<SelectListItem>();
       foreach (SomeThing something in collectionofsomestuff)
       {
           list.Add(new SelectListItem(){Text = something.Name,Selected = false,Value = something.SomeGuid.ToString()});
       }

this is the only way I could get it to work..it wasn't what i was hoping to do tough..

Sevki
A: 

Create a constructor for SelectListItem that accepts your Value as a Guid and ToString it there. Now call your query like so:

from something in collectionofsomestuff select new SelectListItem(something.Name, something.SomeGuid, false);
BigChrisDiD