LINQ Guid toString()

烈酒焚心 提交于 2020-01-12 06:52:45

问题


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:


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



回答2:


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



回答3:


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



回答4:


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




回答5:


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:


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.



来源:https://stackoverflow.com/questions/2512543/linq-guid-tostring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!