简体   繁体   English

将类型“ System.Int64”转换为类型“ System.Object”

[英]Cast the type 'System.Int64' to type 'System.Object'

I'm trying to export a SQL query result to an CSV file, but i'm having some issues with the cast. 我正在尝试将SQL查询结果导出到CSV文件,但是我在转换时遇到了一些问题。

here's my code : 这是我的代码:

var query = string.Concat((from t in db.employees select t.ID + "," + t.LastName+"\n" ).Take(2000));

return File(new System.Text.UTF8Encoding().GetBytes(query), "text/csv", "Report.csv");

But i have the following error : Additional information: Unable to cast the type 'System.Int64' to type 'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types. 但是我有以下错误: Additional information: Unable to cast the type 'System.Int64' to type 'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types. Additional information: Unable to cast the type 'System.Int64' to type 'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types.

Can you help me on this ? 你能帮我吗? Thanks 谢谢

You may need to query the data and do the string projection in two steps: 您可能需要查询数据并分两步进行字符串投影:

var query = 
    (from t in db.employees 
     select new {t.ID , t.LastName})
    .Take(2000)
    .AsEnumerable();

var csv = string.Concat(query.Select(t => t.ID + "," + t.LastName + "\n"));

return File(new System.Text.UTF8Encoding().GetBytes(csv), "text/csv", "Report.csv");

It looks like EF is unable to translate your string concatenation call to SQL. 看来EF无法将您的字符串连接调用转换为SQL。 You should be able to work around this issue by concatenating in memory: 您应该可以通过串联内存来解决此问题:

var query = string.Concat(
    db.employees
        .Select(e => new {ID, LastName})
        .Take(2000)
        .AsEnumerable() // The rest of the query happens in memory
        .Select(p => p.ID + "," + p.LastName)
);

Just to post my theory: 只是发表我的理论:

var query = string.Concat(
    (from t in db.employees select t.ID + "," + t.LastName+"\n" )
    .Take(2000)
    .ToArray());

The difference is the final .ToArray() . 区别在于最终的.ToArray() string.Concat(IEnumerable<string>) was introduced in .NET 4.0 . .NET 4.0中引入了string.Concat(IEnumerable<string>) Perhaps you are using an older version of .NET . 也许您正在使用.NET的旧版本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “System.Int64”类型的表达式不能用于返回类型“System.Object” - Expression of type 'System.Int64' cannot be used for return type 'System.Object' 从物化的“System.Int32”类型到“System.Int64”类型的指定强制转换无效 - The specified cast from a materialized 'System.Int32' type to the 'System.Int64' type is not valid 实体框架错误-由于实例化值为null,因此强制转换为值类型&#39;System.Int64&#39; - Entity Framework Error - The cast to value type 'System.Int64' failed because the materialized value is null 无法将“System.Int32”类型转换为“System.Object”类型。 LINQ 到实体 - Unable to cast the type 'System.Int32' to type 'System.Object'. LINQ to Entities 可查询 <T> 。订购 <T, object> 抛出:无法将类型“ System.Int16”强制转换为类型“ System.Object” - IQueryable<T>.OrderBy<T, object> throws: Unable to cast the type 'System.Int16' to type 'System.Object' 无法将类型“ System.Int32”强制转换为类型“ System.Object”。 LINQ to Entities仅支持强制转换EDM基本类型或枚举类型 - Unable to cast the type 'System.Int32' to type'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types 无法使用Int64强制转换System.Func`2类型的对象 - Unable to cast object of type System.Func`2 with Int64 当使用 LINQ GroupBy 时,ASP.NET 内核尝试将 object 转换为 System.Int64 - ASP.NET Core tries to cast an object to System.Int64 when using LINQ GroupBy 类型'System.Int32'的表达式不能用于方法'Boolean Equals(System.Object)'的'System.Object'类型的参数 - Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)' 类型&#39;System.Int32&#39;的表达式不能用于方法&#39;Boolean Equals(System.Object)&#39;的类型&#39;System.Object&#39;的参数 - Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM