【问题标题】:MySqlDateTime in LINQ QueryLINQ 查询中的 MySqlDateTime
【发布时间】:2013-05-15 06:49:43
【问题描述】:

我正在尝试将 MySqlDateTime 对象与另一个 MySqlDateTime 对象进行比较,每个对象来自两个不同的表。我正在尝试在 LINQ 查询中完成所有这些操作,例如:

        var prodRowFindQuery =
            from x in production.AsEnumerable()
            where x.Field<MySqlDateTime>("date") == row.Field<MySqlDateTime>("date") &&
                  x.Field<String>("device_id3") == row.Field<String>("device_id3")
            select x;

这给了我一个编译器错误:

运算符“==”不能应用于“MySql.Data.Types.MySqlDateTime”和“MySql.Data.Types.MySqlDateTime”类型的操作数

那么我试试这个:

        var prodRowFindQuery =
            from x in production.AsEnumerable()
            where x.Field<MySqlDateTime>("date").ToString() == row.Field<MySqlDateTime>("date").ToString() &&
                  x.Field<String>("device_id3") == row.Field<String>("device_id3")
            select x;

现在它可以编译了,但是我得到一个运行时错误:

指定的转换无效。

堆栈跟踪:

IthacaDbTransferUtil.exe!IthacaDbTransferUtil.DataTableComparer.CompareTables.AnonymousMethod__0(System.Data.DataRow x = {System.Data.DataRow}) Line 34 C#  
System.Core.dll!System.Linq.Enumerable.WhereEnumerableIterator<System.Data.DataRow>.MoveNext() + 0x9c bytes   
IthacaDbTransferUtil.exe!IthacaDbTransferUtil.DataTableComparer.CompareTables(System.Data.DataTable aggregate = {System.Data.DataTable}, System.Data.DataTable production = {System.Data.DataTable}) Line 39 + 0x8b bytes   C#  
IthacaDbTransferUtil.exe!IthacaDbTransferUtil.DbTransferWindowViewModel.CopyTestReliability() Line 177 + 0x94 bytes C#  
IthacaDbTransferUtil.exe!IthacaDbTransferUtil.DelegateCommand.Execute() Line 78 + 0x1f bytes    C#  
IthacaDbTransferUtil.exe!IthacaDbTransferUtil.DelegateCommand.System.Windows.Input.ICommand.Execute(object parameter = null) Line 158 + 0xa bytes   C#  
PresentationFramework.dll!System.Windows.Controls.Button.OnClick() + 0xaf bytes   
PresentationFramework.dll!System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(System.Windows.Input.MouseButtonEventArgs e) + 0x117 bytes    
PresentationCore.dll!System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate handler, object target) + 0x56 bytes    
PresentationCore.dll!System.Windows.EventRoute.InvokeHandlersImpl(object source, System.Windows.RoutedEventArgs args, bool reRaised) + 0x270 bytes    
PresentationCore.dll!System.Windows.UIElement.ReRaiseEventAs(System.Windows.DependencyObject sender = {System.Windows.Controls.Button}, System.Windows.RoutedEventArgs args = {System.Windows.Input.MouseButtonEventArgs}, System.Windows.RoutedEvent newEvent) + 0x183 bytes     
PresentationCore.dll!System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate handler, object target) + 0x56 bytes    
PresentationCore.dll!System.Windows.EventRoute.InvokeHandlersImpl(object source, System.Windows.RoutedEventArgs args, bool reRaised) + 0x270 bytes    
PresentationCore.dll!System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject sender = {System.Windows.Controls.Button}, System.Windows.RoutedEventArgs args = {System.Windows.Input.MouseButtonEventArgs}) + 0x14e bytes  
PresentationCore.dll!System.Windows.UIElement.RaiseTrustedEvent(System.Windows.RoutedEventArgs args = {System.Windows.Input.MouseButtonEventArgs}) + 0x64 bytes   
PresentationCore.dll!System.Windows.Input.InputManager.ProcessStagingArea() + 0x431 bytes     
PresentationCore.dll!System.Windows.Input.InputProviderSite.ReportInput(System.Windows.Input.InputReport inputReport) + 0xfd bytes    
PresentationCore.dll!System.Windows.Interop.HwndMouseInputProvider.ReportInput(System.IntPtr hwnd, System.Windows.Input.InputMode mode, int timestamp, System.Windows.Input.RawMouseActions actions, int x, int y, int wheel) + 0x410 bytes   
PresentationCore.dll!System.Windows.Interop.HwndMouseInputProvider.FilterMessage(System.IntPtr hwnd = 396436, MS.Internal.Interop.WindowMessage msg = WM_LBUTTONUP, System.IntPtr wParam = 0, System.IntPtr lParam = 34472260, ref bool handled = false) + 0x388 bytes    
PresentationCore.dll!System.Windows.Interop.HwndSource.InputFilterMessage(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled) + 0x7c bytes  
WindowsBase.dll!MS.Win32.HwndWrapper.WndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam, ref bool handled) + 0x14a bytes     
WindowsBase.dll!MS.Win32.HwndSubclass.DispatcherCallbackOperation(object o) + 0x80 bytes      
WindowsBase.dll!System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate callback, object args, int numArgs) + 0x5e bytes  
WindowsBase.dll!MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(object source = {System.Windows.Threading.Dispatcher}, System.Delegate method, object args, int numArgs, System.Delegate catchHandler = null) + 0x47 bytes  
WindowsBase.dll!System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority priority, System.TimeSpan timeout, System.Delegate method, object args, int numArgs) + 0x2bc bytes  
WindowsBase.dll!MS.Win32.HwndSubclass.SubclassWndProc(System.IntPtr hwnd, int msg, System.IntPtr wParam, System.IntPtr lParam) + 0x140 bytes      
[Native to Managed Transition]    
[Managed to Native Transition]    
WindowsBase.dll!System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame frame) + 0x112 bytes   
PresentationFramework.dll!System.Windows.Application.RunInternal(System.Windows.Window window) + 0x17a bytes      
PresentationFramework.dll!System.Windows.Application.Run() + 0x67 bytes   
IthacaDbTransferUtil.exe!IthacaDbTransferUtil.App.Main() + 0x77 bytes   C#  
[Native to Managed Transition]    
[Managed to Native Transition]    
Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() + 0x5a bytes    
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) + 0x285 bytes   
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) + 0x9 bytes     
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x57 bytes      
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() + 0x51 bytes     
[Native to Managed Transition]    

我试过的另一个例子:

            var prodRowFindQuery =
                from x in production.AsEnumerable()
                where Convert.ToDateTime(x.Field<MySqlDateTime>("date")) == Convert.ToDateTime(row.Field<MySqlDateTime>("date")) &&
                      x.Field<String>("device_id3") == row.Field<String>("device_id3")
                select x;

仍然引发运行时错误:

指定的转换无效。

另一个例子:

            var prodRowFindQuery =
                from x in production.AsEnumerable()
                where x.Field<DateTime>("date") == row.Field<DateTime>("date") &&
                      x.Field<String>("device_id3") == row.Field<String>("device_id3")
                select x;

同样的运行时错误:

指定的转换无效。

有人说“日期”不是 MySqlDateTime 类型,为了好玩,这是它的证明:

            var prodRowFindQuery =
                from x in production.AsEnumerable()
                where x.Field<String>("date") == row.Field<String>("date") &&
                      x.Field<String>("device_id3") == row.Field<String>("device_id3")
                select x;

运行时异常:

无法将“MySql.Data.Types.MySqlDateTime”类型的对象转换为“System.String”类型。

我尝试了其他一些方法,但没有任何效果。

任何帮助将不胜感激,

问候,

凯尔

【问题讨论】:

  • 请添加你得到的异常的堆栈跟踪。
  • .ToString() 可能不支持没有格式操作数。你试过 .Equals() 吗?
  • 引发异常时的确切位置在哪里?是在.Field&lt;...&gt;() 通话中吗?
  • MD.Unicorn:引发此异常的位置是当我将“prodRowFindQuery”放入 foreach 循环中时。此异常在此 foreach 循环的第一个循环中引发;它甚至没有运行,因为它在尝试拉出“日期”字段时引发了异常
  • 你说的是“linq”,但没有具体说明什么味道。这个实体框架是否带有 MySql 后端? Linq 到 Sql? MySql 特定的东西?请澄清。

标签: c# mysql .net datetime casting


【解决方案1】:

我终于解决了这个问题。

在伪代码中,我是这样做的:

  1. 新建两个数据表
  2. 在一个 DataTable 上进行选择,提取该表所需的所有数据
  3. 通过这样做克隆表:

        officialTable = temporaryTable.Clone();
    
  4. 通过这样做来投射列:

        officialTable.Columns["date"].DataType = typeof(String);
    
  5. 要么遍历表行,将一行复制到另一行;或者执行另一个 select 语句再次获取所有数据,但这一次,“数据”列将自动转换为 a 而不是 .

  6. 废弃旧数据表

我希望这对其他人有所帮助。

【讨论】:

    【解决方案2】:

    如果没有为 MySQLDateTime 定义相等运算符,您可以自己定义一个,或者在进行比较之前将其转换为标准 .NET 日期时间对象。

    【讨论】:

    • 查看我在原始帖子的堆栈跟踪之后发布的新代码示例。如何在 LINQ 查询中成功地将 MySqlDateTime 转换为 System.DateTime?
    【解决方案3】:

    最高概率是DataRow"date" 字段不是MySqlDateTime 类型,或者"device_id3" 字段不是string 类型。

    【讨论】:

    • 这里有更多信息可能会有所帮助:我知道“device_id3”绝对是一个字符串。至于日期,我不完全确定。我总是收到 MySqlDateTime 转换错误,我觉得这很奇怪,因为当我查询该列时,我说“CAST(date as CHAR(50)) AS date”将 DateTime 字段转换为字符串字段,然后再进入.NET。
    猜你喜欢
    • 2012-02-06
    • 1970-01-01
    • 2020-08-21
    • 2010-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-13
    相关资源
    最近更新 更多