LINQ Guid toString()

2020-06-09 04:37发布

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?

6条回答
放荡不羁爱自由
2楼-- · 2020-06-09 05:07

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

查看更多
够拽才男人
3楼-- · 2020-06-09 05:08

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}; 
查看更多
forever°为你锁心
4楼-- · 2020-06-09 05:09

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
        })
查看更多
啃猪蹄的小仙女
5楼-- · 2020-06-09 05:10

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);
查看更多
家丑人穷心不美
6楼-- · 2020-06-09 05:11

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};
查看更多
你好瞎i
7楼-- · 2020-06-09 05:17

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.

查看更多
登录 后发表回答