System.InvalidCastException: 指定的转换无效 Linq ToList()

本文详细解释了在使用LINQ to SQL时,由于DateTime类型与数据库中Nvarchar类型的不匹配,导致出现指定的转换无效错误的原因,并提供了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
System.InvalidCastException: 指定的转换无效
 System.Data.Linq.Table<Model>
ToList()方法
报出
System.InvalidCastException: 指定的转换无效错误
经过网上搜集资料,发现是因为linqtosql使用的Model字段类型和数据库的字段类型不匹配导致的,经过仔细对照才发现问题,改成相匹配的就行了
我这的问题是Model中的属性为DateTime类型而数据库中为Nvarchar类型
代码优化 using System; using System.IO; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; using System.Runtime.InteropServices; namespace EMCL { //public interface IScriptVM_Console //{ // bool WriteDebug(string info); // bool WriteInfo(string info); // bool WriteWarn(string info); // bool WriteError(string info); // bool WriteException(Exception e); // string ReadLine(); //}; public interface IScriptVM : IDisposable { SimpleLogger Logger { get; set; } Assembly HostAssembly { get; set; } ICollection<string> SearchPath { get; } ICollection<string> PreloadAssembly { get; } bool VM_Init(); bool VM_Exit(); bool VM_Renew(); void VM_LogException(Exception e); //void VM_SetConsole(IScriptVM_Console con); bool VM_LoadStdLib(string name); string DefaultScriptPath { get; set; } bool VM_LoadFile(string sFile); object VM_Find(string name); bool VM_List(ref Dictionary<object, object> vars); object VM_Run(string sLine); dynamic VM_Call(string name, params object[] args); //void VM_SetVar(string name, object obj); //dynamic VM_GetVar(string name); //void VM_DelVar(string name); //bool VM_HasVar(string name); //string[] VM_List(); //Dictionary<object, object> VM_ListVars(); } public class CScriptVM { static public IScriptVM CreateVM(string sLanguage) { string sClassName = "SimpleScript.ScriptVM_Impl"; string sFuncName = "CreateVM"; string sDrvPath = sLanguage; string sDrvName = sLanguage; if (!Path.IsPathRooted(sDrvPath)) { String path = AppDomain.CurrentDomain.BaseDirectory; sDrvPath = path + "SimpleScriptVM_" + sLanguage + ".dll"; } else { sDrvName = ""; } object[] ps = null;// new object[1] { sDrvName }; object res = PluginLoader.CallAssemblyFunction(sDrvPath, sClassName, sFuncName, ps); if (null == res) return null; return (IScriptVM)res; } } }
06-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值