2015-08-18 45 views
0

我有以下代碼。數據來自mysql服務器。我使用dataadapater.Fill(datatable)對象和方法來創建數據表CDMAData。每次數據範圍不同時,column.datatype不同。我需要能夠動態地指定數據類型。可能嗎?錯誤發生在r.Field < int>。C#Datatable LINQ動態row.field <datatype>需要編碼

var CarrierCDMAPreResults = from row in CDMAData.AsEnumerable() 
    where Dates.Contains(row.Field<DateTime>("DateTime")) 
     group row by new { Cascade = row.Field<string>("Cascade"), Sector = row.Field<byte>("Sector"), Carrier = row.Field<byte>("Carrier") } into grp 
    select new 
    { 
     Cascade = grp.Key.Cascade, 
     Sector = grp.Key.Sector,            
     Carrier = grp.Key.Carrier, 
     Attempts = grp.Sum(r => r.Field <int> ("Attempts")), 
     Successes = grp.Sum(r => r.Field<int>("Successes")), 
     Blocks = grp.Sum(r => r.Field<int>("Blocks")), 
     Drops = grp.Sum(r => r.Field<int>("Drops")), 
     CDMAPE = grp.Sum(r => r.Field<decimal>("CDMA Primary Erlang")) 
    }; 
+0

你是什麼意思你想動態地指定數據類型?數據類型將始終與sql查詢的結果相同。 – CathalMF

+0

根據數據,該表爲同一列返回不同的數據類型。它可以是uint32,int32,uint64,int64 –

+0

您是否使用相同的代碼來處理不同查詢的結果集? – CathalMF

回答

1

對你的問題,一個非常簡單的答案將是隻使用Int64的一切。由於所有較低的值仍然有效。

但是,如果你的列開始變成小數和字符串,那麼你將有問題。

var CarrierCDMAPreResults = from row in CDMAData.AsEnumerable() 
    where Dates.Contains(row.Field<DateTime>("DateTime")) 
     group row by new { Cascade = row.Field<string>("Cascade"), Sector = row.Field<byte>("Sector"), Carrier = row.Field<byte>("Carrier") } into grp 
    select new 
    { 
     Cascade = grp.Key.Cascade, 
     Sector = grp.Key.Sector,            
     Carrier = grp.Key.Carrier, 
     Attempts = grp.Sum(r => r.Field<Int64> ("Attempts")), 
     Successes = grp.Sum(r => r.Field<Int64>("Successes")), 
     Blocks = grp.Sum(r => r.Field<Int64>("Blocks")), 
     Drops = grp.Sum(r => r.Field<Int64>("Drops")), 
     CDMAPE = grp.Sum(r => r.Field<decimal>("CDMA Primary Erlang")) 
    }; 
+0

謝謝我最終確保表格列是通過更改查詢和鑄造文件而形成的。然後我用了很久(Int64)。這是工作。 –