【问题标题】:How I get the right Data from my Column with Linq我如何使用 Linq 从我的列中获取正确的数据
【发布时间】:2012-11-24 05:26:42
【问题描述】:

我的 Linq 不太好,我想进行查询以获取正确的数据。我有一个包含这些列的数据表:

我想通过 TIMEFROM 和 TIMETO 列进入数据表得到这个 = 10.12.2012 不是 10.12.2012 00:00:00。

我该怎么做:我的数据表的名称是 table。

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;

【问题讨论】:

  • 列是什么意思?您只想要日期并缩短它们还是按时间排序的整个表格?
  • 认为图片上传不正确。
  • 要从 DateTime 对象中省略小时,请使用 .ToString("d")
  • 最好是 myVar.ToShortDateString()
  • 获得数据后:dateTime = dateTime.ToShortDateString();

标签: c# asp.net sql linq datatable


【解决方案1】:

你可能想使用

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

【讨论】:

    【解决方案2】:

    您必须先创建上下文类而不是创建对象,然后才能在其上运行 linq 命令。 例如,您的表名是 test,而不是您创建 Linq to Sql 类文件,将表 test 拖到上面。 比在您的主程序或您需要调用它的地方。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多