Sevki
Sevki

Reputation: 3682

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?

Upvotes: 13

Views: 10083

Answers (6)

thaBadDawg
thaBadDawg

Reputation: 5220

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:

[DataContract]
public class DeviceInfo
{
    public Guid DeviceGuid
    {
        set
        {
            DeviceID = value.ToString();
        }
    }
    [DataMember]
    public string DeviceID { get; set; }
}

And the query works as designed because it has something else doing the conversion for it:

devices.AddRange(from d in ae.UserDevices
                    select new DeviceInfo
                    {
                        DeviceGuid = d.DeviceID
                    }

It makes the object a little messier, but makes dealing with the Guid in the query so much easier.

Upvotes: 1

gxl
gxl

Reputation: 61

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}; 

Upvotes: 6

BigChrisDiD
BigChrisDiD

Reputation: 152

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

Upvotes: 1

Sevki
Sevki

Reputation: 3682

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..

Upvotes: 1

spender
spender

Reputation: 120400

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

Upvotes: 4

Mike
Mike

Reputation: 327

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};

Upvotes: 6

Related Questions