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

在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#的严格性!


慕沐林林
浏览 729回答 3
3回答

qq_花开花谢_0

使用EF v4,您可以使用SqlFunctions.StringConvert..INT没有过载,所以您需要转换为双小数或小数点。您的代码最后看起来如下所示:var items = from c in contacts            select new ListItem             {                 Value = SqlFunctions.StringConvert((double)c.ContactId).Trim(),                 Text = c.Name             };
打开App,查看更多内容
随时随地看视频慕课网APP