问题与LINQ的INT转换为字符串实体 [英] Problem with converting int to string in Linq to entities

查看:412
本文介绍了问题与LINQ的INT转换为字符串实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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是灵活的,即时通讯无法习惯了C#的严格!

Is there anyway I can achieve this? Note, that in VB.NET there is no problem use the first snippet it works just great, VB is flexible, im unable to get used to C#'s strictness!!!

推荐答案

使用EF V4可以使用<一个href=\"http://msdn.microsoft.com/en-us/library/dd466166.aspx\"><$c$c>SqlFunctions.StringConvert.有没有超载对于int,所以你需要转换为double或十进制。您code最终看起来是这样的:

With EF v4 you can use SqlFunctions.StringConvert. There is no overload for int so you need to cast to a double or a decimal. Your code ends up looking like this:

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

这篇关于问题与LINQ的INT转换为字符串实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆