首页 新闻 赞助 找找看

有关linq的使用问题

0
悬赏园豆:20 [已关闭问题] 关闭于 2012-02-22 16:33

 DataContext ctx = new DataContext("data source=.;user id=sa;password=123456;initial catalog=lemp;Connect Timeout=9999;Max Pool Size=75; Min Pool Size=5");

Table<Company> Customers = ctx.GetTable<Company>();

var com=from c in Customers where c.id == 22 select new { id = c.id, 顾客名 = c.coname, 城市 = c.coaddress };
        foreach (var item in com)
        {
            string hah = item.城市;
        }

 

在foreach编历时 抛出异常 说指定的转换无效

是怎么回事?

 

注: 数据库表 company 中字段Id 是int类型 我如果在创造匿名类型

new { id = c.id, 顾客名 = c.coname, 城市 = c.coaddress }中 去掉 id = c.id  就可以执行成功

 

问题补充:

我把类


    [Table(Name = "Company")]
    public class Company
    {
        //[Column(IsPrimaryKey = true)]
        [Column]
        public Int64 id { get; set; }

        [Column(Name = "coname")]
        public string coname { get; set; }

        [Column]
        public string coaddress { get; set; }

    }

中的id类型写成Int64

在前台编历时这样写

var s=from c in Customers where c.id == 22 select new { id = c.id };
        foreach (var item in s)
        {
            Int64 hah = item.id;
        }

 

接收类型也是 Int64

程序可以执行 为什么 写成int 就不可以了呢????

holly Yang的主页 holly Yang | 初学一级 | 园豆:10
提问于:2012-02-22 15:32
< >
分享
所有回答(2)
0

coaddress在数据库中是什么类型,有空值吗?如果有空值把空值筛选掉试试

artwl | 园豆:16736 (专家六级) | 2012-02-22 15:40
0

具体异常信息贴出来,哪一行报的什么错。

水牛刀刀 | 园豆:6350 (大侠五级) | 2012-02-22 15:40

   在 System.Data.SqlClient.SqlBuffer.get_Int32()
   在 System.Data.SqlClient.SqlDataReader.GetInt32(Int32 i)
   在 Read_<>f__AnonymousType0`1(ObjectMaterializer`1 )
   在 System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()
   在 System.Linq.SystemCore_EnumerableDebugView`1.get_Items()

 

是在  foreach (var item in com)
        {
            string hah = item.城市;
        }

编历时报的错

coname

coaddress都是 string类型 如果 在

var com=from c in Customers where c.id == 22 select new { id = c.id, 顾客名 = c.coname, 城市 = c.coaddress };这一句中去掉 id = c.id,部分 就可以遍历了, id 是int类型

支持(0) 反对(0) holly Yang | 园豆:10 (初学一级) | 2012-02-22 15:46
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册