为了账号安全,请及时绑定邮箱和手机立即绑定

在Linq中将int转换为String时遇到的问题

在Linq中将int转换为String时遇到的问题

C#
慕沐林林 2019-06-28 10:39:00
在Linq中将int转换为String时遇到的问题var items = from c in contacts            select new ListItem             {                 Value = c.ContactId, //Cannot implicitly convert type 'int' (ContactId) to 'string' (Value).                 Text = c.Name             };var items = from c in contacts            select new ListItem             {                 Value = c.ContactId.ToString(), //Throws exception: ToString is not supported in linq to entities.                 Text = c.Name             };我能做到吗?请注意,在VB.NET中使用第一段没有问题-它的工作非常棒,VB很灵活,im无法适应C#的严格性!
查看完整描述

3 回答

?
qq_花开花谢_0

TA贡献1835条经验 获得超7个赞

使用EF v4,您可以使用SqlFunctions.StringConvert..INT没有过载,所以您需要转换为双小数或小数点。您的代码最后看起来如下所示:

var items = from c in contacts            select new ListItem
            {
                Value = SqlFunctions.StringConvert((double)c.ContactId).Trim(),
                Text = c.Name
            };


查看完整回答
反对 回复 2019-06-28
  • 3 回答
  • 0 关注
  • 728 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信