简体   繁体   English

无法将类型“ 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

Hi I believe this question was already a 100 times on StackOverFlow, but I did not manage to fix my issue. 嗨,我相信这个问题在StackOverFlow上已经是100次了,但是我没有解决此问题。 This is code line that makes me a trouble 这是使我麻烦的代码行

 DatumSnimanja = (x.VrijemeSnimanja != null) ? ((DateTime)x.VrijemeSnimanja).Day + "." + ((DateTime)x.VrijemeSnimanja).Month + "." + ((DateTime)x.VrijemeSnimanja).Year : null,

Please how to fix this. 请解决此问题。

full code 完整的代码

     var result = resultVideoKlipBaseInfo.Select(x => new
            {
                x.IDvideoKlip,
                x.LokacijaHDD,
                x.NazivVideoKlipa,
                x.Snimatelj.IDsnimatelj,
                x.Snimatelj.Ime,
                x.Snimatelj.Prezime,
                x.Snimatelj.Storno,
                DatumSnimanja = (x.VrijemeSnimanja != null) ? ((DateTime)x.VrijemeSnimanja).Day + "." + ((DateTime)x.VrijemeSnimanja).Month + "." + ((DateTime)x.VrijemeSnimanja).Year : null,
                SatIMinutaSnimanja = (x.VrijemeSnimanja != null) ? ((DateTime)x.VrijemeSnimanja).Hour + ":" + ((DateTime)x.VrijemeSnimanja).Minute : null,
                x.VrijemeSnimanja,                          
                x.VrijemeTrajanjaKlipa

            });
            return Ok(result);

With this code I am fetching DateTime from database, and casting it to string format from DateTime format. 通过此代码,我将从数据库中获取DateTime,并将其从DateTime格式转换为字符串格式。

I want to have from this 2016-12-31 00:00:00.000 something like this 31.12.2016 我想从这里2016-12-31 00:00:00.000像这样31.12.2016

Thank you all for reading this question :) 谢谢大家阅读这个问题:)

You can't use ToString() for formatting inside a query going against a database because there is no equivalent SQL method and it will give you an error to that effect. 您不能使用ToString()在针对数据库的查询中进行格式化,因为没有等效的SQL方法,并且这样做会给您带来错误。 (Here is a list of the string methods supported, BTW: https://msdn.microsoft.com/en-us/library/bb738534(v=vs.100).aspx ). (以下是所支持的字符串方法的列表,顺便说一句: https ://msdn.microsoft.com/zh-cn/library/bb738534( v=vs.100 ).aspx)。

The easiest approach is return the raw values, and then do that formatting after the main query is called (by calling ToList() on it, or iterating through the results). 最简单的方法是返回原始值,然后在调用主查询后进行格式化(通过在其上调用ToList()或遍历结果)。

Here are a few ways: 以下是几种方法:

How can I convert DateTime to String in Linq Query? 如何在Linq查询中将DateTime转换为String?

Convert datetime to a formatted string inside a LINQ to SQL query 将日期时间转换为LINQ to SQL查询中的格式化字符串

I fix this by changing line of code 我通过更改代码行来解决此问题

DatumSnimanja = (x.VrijemeSnimanja != null) ? ((DateTime)x.VrijemeSnimanja).Day + "." + ((DateTime)x.VrijemeSnimanja).Month + "." + ((DateTime)x.VrijemeSnimanja).Year : null,

In to this 在这个

    DatumSnimanja = SqlFunctions.DateName("day", x.VrijemeSnimanja) + "." + SqlFunctions.DateName("month", x.VrijemeSnimanja) + "." +  SqlFunctions.DateName("year", x.VrijemeSnimanja),

Thank you all for assistance ! 谢谢大家的帮助! :) :)

暂无
暂无

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

相关问题 无法将类型“ System.Int32”强制转换为类型“ System.Object”。 LINQ to Entities仅支持转换实体数据模型原语类型 - Unable to cast the type 'System.Int32' to type 'System.Object'. LINQ to Entities only supports casting Entity Data Model primitive types 无法将类型“ System.String”强制转换为类型“ System.Object”。 LINQ to Entities仅支持转换实体数据模型原语类型 - Unable to cast the type 'System.String' to type 'System.Object'. LINQ to Entities only supports casting Entity Data Model primitive types 无法将System.Double强制转换为System.Object。 LINQ to Entities仅支持强制转换EDM基本类型或枚举类型 - Unable to cast System.Double to System.Object. LINQ to Entities only supports casting EDM primitive or enumeration types 无法将类型X强制转换为类型Y.LINQ to Entities仅支持强制转换EDM原语或枚举类型 - Unable to cast the type X to type Y. LINQ to Entities only supports casting EDM primitive or enumeration types 无法将类型 A 转换为类型 B。LINQ to Entities 仅支持转换 EDM 原语或枚举类型 - Unable to cast the type A to type B. LINQ to Entities only supports casting EDM primitive or enumeration types 无法将类型“System.Int64”强制转换为“System.Object”类型。 LINQ to Entities仅支持转换实体数据模型基元类型 - Unable to cast the type 'System.Int64' to type 'System.Object'. LINQ to Entities only supports casting Entity Data Model primitive types 无法将类型“ Shared.ClientService”强制转换为类型“ Shared.IClientID”。 LINQ to Entities仅支持强制转换EDM基本类型或枚举类型 - Unable to cast the type 'Shared.ClientService' to type 'Shared.IClientID'. LINQ to Entities only supports casting EDM primitive or enumeration types LINQ to Entities仅支持强制转换EDM原语或枚举类型错误 - LINQ to Entities only supports casting EDM primitive or enumeration types error Cast()例外:LINQ to Entities仅支持强制转换EDM基本类型或枚举类型 - Cast() exception: LINQ to Entities only supports casting EDM primitive or enumeration types 无法将类型为“WhereListIterator`1[System.Object]”的对象转换为类型“System.Collections.Generic.IEnumerable`1[System.Int32]” - Unable to cast object of type 'WhereListIterator`1[System.Object]' to type 'System.Collections.Generic.IEnumerable`1[System.Int32]'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM