2013-05-20 22 views
5

我正在嘗試將MySqlDateTime對象與另一個MySqlDateTime對象(每個對象來自兩個不同的表)進行比較。我試圖做到這一切LINQ查詢裏面,例如:LINQ查詢中的MySqlDateTime

 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; 

現在它編譯,但我得到一個運行時間E rror:

指定的轉換無效。

堆棧跟蹤:

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'。

我已經嘗試了一些其他的東西,沒有爲我工作。

任何幫助將不勝感激,

問候,

凱爾

+0

請添加您得到的異常的堆棧跟蹤。 –

+0

堆棧跟蹤已添加 – kformeck

+0

。如果沒有格式操作數,則可能不支持ToString()。你有沒有試過.Equals()? –

回答

0

我終於解決了這個問題。

在僞代碼,這裏是我是如何做的:

  1. 創建兩個新的數據表
  2. 做一個選擇上的一個數據表,在表需要的
  3. 克隆表中的所有數據拉通過這樣做:

    0123:

    officialTable = temporaryTable.Clone(); 
    
  4. 做這個轉換列

    officialTable.Columns["date"].DataType = typeof(String); 
    
  5. 或者遍歷錶行,將一行復制到另一行;或者做另一個選擇語句來重新獲得所有數據,但是這一次,「數據」列將被自動轉換爲一個而不是。舊的數據表

希望的

  • 處置這可以幫助別人了。

  • 0

    的最高可能性是的DataRow"date"場不MySqlDateTime類型,或者"device_id3"string型的不。

    +0

    繼承人多一點信息,可能會有所幫助:我知道「device_id3」絕對是一個字符串的事實。至於日期,我不完全確定。我總是得到MySqlDateTime鑄造錯誤,我發現這很奇怪,因爲當我查詢該列時,我會說「CAST(日期爲CHAR(50))AS date」將DateTime字段強制轉換爲字符串字段,然後才能到達.NET。 – kformeck

    0

    如果沒有爲MySQLDateTime定義等號運算符,您可以自己定義一個,或者在進行比較之前將其轉換爲標準.NET日期時間對象。

    +0

    查看我在原始帖子中的堆棧跟蹤之後發佈的新代碼示例。我如何成功從MySqlDateTime轉換爲LINQ查詢中的System.DateTime? – kformeck