2012-12-06 36 views
0

Linq我不太好,我想查询以获取正确的数据。我有一个DataTable与此列:我如何从Linq的Column中获取正确的数据

enter image description here

我想通过列TIMEFROM和TIMETO DataTable中获得获得此= 2012年12月10日 不是2012年12月10日00:00:00。

我该如何做到这一点:我的数据表的名称是表。

MySqlCommand selectcommand = new MySqlCommand(sqlcommand, connection); 

       MySqlDataAdapter adapter = new MySqlDataAdapter(selectcommand); 

       DataTable table = new DataTable(TABLE); 


       adapter.Fill(table); 

       table = table.AsEnumerable().Where(r => r.Field<int>("Activate") == 1).CopyToDataTable(); 



       //i need the shorter form 



       connection.Open(); 

       selectcommand.ExecuteReader(); 

       return table; 
+0

你是什么意思的列?你想只是日期,并缩短它们或整个表从时间到? –

+0

认为图像没有正确上传。 –

+0

若要从DateTime对象中省略小时,请使用.ToString(“d”) –

回答

2

您可能需要使用

String.Format("{0:dd.MM.yyyy}", dataTable.Rows[0]["TIMEFROM"]); 
String.Format("{0:dd.MM.yyyy}", dataTable.Rows[0]["TIMETO"]); 
0

你必须创建contextclass不是创建对象之前,你可以在其上运行的LINQ命令。 例如,你的表名是测试比你创建Linq到Sql类文件,拖动表测试它。 比你的主程序或任何你需要调用它。

 testContext ct=new testContext(); 
     var er=from e in ct.test 
     Select new(e.TIMEFROM, e.TIMETO); 
相关问题