代码
var rows = (
                    from product in products
                    select new
                    {
                        i= product.fid.ToString(),
                        cell = new string[] {
                         product.fid.ToString(),
                         product.fcode,
                         product.fno,
                         product.fspec
                      }
                    }).ToArray();
编译时不出错,运行时出错
报错: LINQ to Entities 不识别方法“System.String ToString()”,因此该方法无法转换为存储表达式。
要怎么解决啊?

解决方案 »

  1.   

    Linq里面是不是需要这样转换:i = SqlFunctions.StringConvert((int)fid)
      

  2.   

    其实你可以试试不使用.ToString(),看看是否可以正常运行?如果可以运行,则该值实际为string型,没必要再.ToString(),或者你最好使用如下方式试试:var rows = products.AsEnumerable().
    select(product => new{ 
    product = n.fid,
    cell=new string[]
    {
    n.fid.ToString(), 
    n.fcode,
    n.fno,
    n.fspec
    }
    }
    ).ToArray();
      

  3.   

    var rows = (
                         from product in products
                         select new
                         {
                             i= product.fid, //.ToString(),
                             cell = new string[] {
                              product.fid,  //.ToString(),
                              product.fcode,
                              product.fno,
                              product.fspec
                           }
                         }).ToArray();
      

  4.   

    需要tostring的,实现在 linq表达式的外面处理好,tostring不被转成sql,也就是说sql不认识tostring方法,linq也不做转化处理
      

  5.   

    tanghuawei,gxingmin 你们说的方法,我试过了都不行
    fid这个字段是一个int型的字段
    所以
    cell = new string[] {
                         product.fid,  //.ToString(),   ==>不转换成string,会报错的
                         product.fcode,
                         product.fno,
                         product.fspec
                        }
      

  6.   

    hjywyj你的方法报错
    无法将类型“System.Int64”强制转换为类型“System.Object”。LINQ to Entities 仅支持强制转换 EDM 基元或枚举类型。
      

  7.   


    顶,前段时间开发大量使用了linq,确实如此,linq好象只支持可以在CLR里面可以执行的方法。另外,对hjywyj的写法很感兴趣,个人觉得说不定能成。可惜我开发的时候,没想到这个
      

  8.   

    厄,....
    hjywyj的写法,不行啊
      

  9.   

    先转化为List然后在select.在LinqToObject中就可以转换了!
      

  10.   

    ToString不是标准函数,而String.Contace只映射了两个接受string作为参数的重载
    如果你是用SQL的话,可以试试SqlFunctions.StringConvert
    http://msdn.microsoft.com/en-us/library/dd466301.aspx
      

  11.   

    To: hhddzzvar rows = (
                        from product in products
                        select new
                        {
                            i=SqlFunctions.StringConvert( product.fid),   
                            cell = new string[] {
                             SqlFunctions.StringConvert(product.fid),  ==>这样子,编译报错啊
                             product.fcode,
                             product.fno,
                             product.fspec
                          }
                        }).ToArray();