嗨,这看起来挺管用的,
from something in collectionofsomestuff
select new SelectListItem(){Text = something.Name, Value = something.SomeGuid.ToString(), Selected = false};当我尝试这样做时,它不起作用,给我错误
LINQ到实体不识别方法'System.String ToString()‘方法,并且该方法不能转换为存储表达式。
有解决办法吗?
发布于 2010-03-25 01:20:38
并不是所有的CLR方法都可以与Linq到实体一起使用.ToString()似乎就是其中之一。
看看正则函数映射的CLR方法。
也许尝试将GUID显式地设置为一个字符串变量,在Linq之外。
string myGuid = SomeGuid.ToString();
from something in collectionofsomestuff
select new SelectListItem(){Text = Name, Value = myGuid, Selected = false};发布于 2011-03-11 08:44:45
您可以在db中获取记录,然后将它们转换为使用ToList()或ToArray().Then的列表或数组--使用对象。例如(它是LINQ实体):
var list = collectionofsomestuff.select(c => c).ToList();
from something in list
select new SelectListItem(){Text = something.Name, Value = something.SomeGuid.ToString(), Selected = false}; 发布于 2010-03-25 01:26:01
我的Linq查询表达式说得不太好,但下面的方法应该能做到这一点:
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
})https://stackoverflow.com/questions/2512543
复制相似问题