我想使用唯一列值减去2个数据表的列值并分配新的结果数据表。Substract 2 datatables using linq
public void getAllTopActivityData()
{
dtTopAllTemp.Clear();
odagetTopAllTemp = new OracleDataAdapter(getTopAll, oradb);
odagetTopAllTemp.Fill(dtTopAllTemp);
if (isFirst == false)
{
isFirst = true;
dtTopAllTempOld = dtTopAllTemp;
}
var result = (from dr1 in dtTopAllTempOld.AsEnumerable()
from dr2 in dtTopAllTemp.AsEnumerable()
where dr1.Field<string>("EVENT") == dr2.Field<string>("EVENT")
select new
{
SAMPLE_TIME = dr1.Field<DateTime>("SAMPLE_TIME"),
INST_ID = dr1.Field<int>("INST_ID"),
WAIT_CLASS = dr1.Field<string>("WAIT_CLASS"),
EVENT = dr1.Field<string>("EVENT"),
WAITS = ((dr2["WAITS"] == DBNull.Value ? 0 : dr2.Field<double>("WAITS")) - (dr1["WAITS"] == DBNull.Value ? 0 : dr1.Field<double>("WAITS")))
}).ToList();
ultraGrid1.DataSource = result;/This doesn't work.
-- ultraGrid1.DataSource = dtTopAllTemp;/This brings data.
-- ultraGrid1.DataSource = dtTopAllTempOld;/This brings data. All it works.
dtTopAllTempOld = dtTopAllTemp;
}
但是,我收到了'Invalid Cast Exception'错误。我再次检查了所有列数据类型是否正常。
konum: System.Data.DataRowExtensions.Field[T](DataRow row, String columnName)
konum: Oratify.Main.&lt;getAllTopActivityData&gt;b__188_3(&lt;&gt;f__AnonymousType0`2 &lt;&gt;h__TransparentIdentifier0) C:\Users\sezer\Desktop\AllC\Oratify\Oratify\Oratify\Main.cs içinde: satır 1495
konum: System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
konum: System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
konum: System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
konum: Oratify.Main.getAllTopActivityData() C:\Users\sezer\Desktop\AllC\Oratify\Oratify\Oratify\Main.cs içinde: satır 1492
konum: Oratify.Main.timer_TimerTopSQL(Object sender, EventArgs e) C:\Users\sezer\Desktop\AllC\Oratify\Oratify\Oratify\Main.cs içinde: satır 1534
konum: System.Windows.Forms.Timer.OnTick(EventArgs e)
konum: System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message&amp; m)
konum: System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
konum: System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)
konum: System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
konum: System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
konum: System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
konum: System.Windows.Forms.Application.Run(Form mainForm)
konum: Oratify.Program.Main() C:\Users\sezer\Desktop\AllC\Oratify\Oratify\Oratify\Program.cs içinde: satır 19</StackTrace><ExceptionString>System.InvalidCastException: Belirtilen atama geçerli değil.
konum: System.Data.DataRowExtensions.UnboxT`1.ValueField(Object value)
konum: System.Data.DataRowExtensions.Field[T](DataRow row, String columnName)
konum: Oratify.Main.&lt;getAllTopActivityData&gt;b__188_3(&lt;&gt;f__AnonymousType0`2 &lt;&gt;h__TransparentIdentifier0) C:\Users\sezer\Desktop\AllC\Oratify\Oratify\Oratify\Main.cs içinde: satır 1495
konum: System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
konum: System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
konum: System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
konum: Oratify.Main.getAllTopActivityData() C:\Users\sezer\Desktop\AllC\Oratify\Oratify\Oratify\Main.cs içinde: satır 1492
konum: Oratify.Main.timer_TimerTopSQL(Object sender, EventArgs e) C:\Users\sezer\Desktop\AllC\Oratify\Oratify\Oratify\Main.cs içinde: satır 1534
konum: System.Windows.Forms.Timer.OnTick(EventArgs e)
konum: System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message&amp; m)
konum: System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
konum: System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)
konum: System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
konum: System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
konum: System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
konum: System.Windows.Forms.Application.Run(Form mainForm)
konum: Oratify.Program.Main() C:\Users\sezer\Desktop\AllC\Oratify\Oratify\Oratify\Program.cs içinde: satır 19</ExceptionString></Exception></TraceRecord>
您是否检查过您的db中“dr2.Field(”WAITS“)”和dr1.Field (“WAITS”)的所有值? –
检查你的日期时间,铸造前的双字段为空 –