针对DataTable运行SQL查询

时间:2019-03-08 13:12:21

标签: c# sql winforms linq datatable

我正在像这样填充数据表

static DataTable GetTable()
{
    DataTable table = new DataTable();
    table.Columns.Add("name", typeof(string));
    table.Columns.Add("exam1date", typeof(date));
    table.Columns.Add("exam1complete", typeof(date));
    table.Columns.Add("exam2date", typeof(date));
    table.Columns.Add("exam2complete", typeof(date));
    table.Columns.Add("exam3date", typeof(date));
    table.Columns.Add("exam3complete", typeof(date));

    table.Rows.Add("joe", "2018-01-30", NULL, NULL, NULL, NULL, NULL);
    table.Rows.Add('james', '2018-02-14', '2018-02-21', '2018-03-02', NULL, NULL, NULL);
    table.Rows.Add('javier', '2018-01-01', '2018-01-14', '2018-03-01', '2018-03-12', '2018-04-01', NULL);
    return table;
}


static void Main()
{
    DataTable dtInfo = GetTable();
}

如何对DataTable运行此SQL语句,以我可以使用的格式从数据表中获取数据?

SELECT fname,  
    CASE 
    WHEN exam1complete IS NULL THEN 'exam1date'
    WHEN exam2complete IS NULL THEN 'exam2date '
    ELSE 'exam3date' END AS columnname,
    CASE
    WHEN exam1complete IS NULL then exam1date 
    WHEN exam2complete IS NULL then exam2date 
    ELSE exam3date END AS examdate
FROM dtInfo;

1 个答案:

答案 0 :(得分:0)

看起来有点奇怪,但是如果将null更改为DateTime.MinValue,下面的代码似乎可以正常工作...

class Program
{
    static void Main(string[] args)
    {
        DataTable dtInfo = GetTable();


        var query =
            from row in dtInfo.AsEnumerable() select new
            {
              name =  row.Field<string>("name"),
              columnname = 
              (row.Field<DateTime>("exam1complete")==DateTime.MinValue) 
              ? 
              ("exam1date") 
              :
              (
                (row.Field<DateTime>("exam2complete") == DateTime.MinValue) ? ("exam2date") : ("exam3date")
              ),
              examdate =
              (row.Field<DateTime>("exam1complete") == DateTime.MinValue)
              ?
              (row.Field<DateTime>("exam1date"))
              :
              (
                (row.Field<DateTime>("exam2complete") == DateTime.MinValue) ? (row.Field<DateTime>("exam2date")) : (row.Field<DateTime>("exam3date"))
              )
            }
            ;
    }

    static DataTable GetTable()
    {
        DataTable table = new DataTable();
        table.Columns.Add("name", typeof(string));
        table.Columns.Add("exam1date", typeof(DateTime));
        table.Columns.Add("exam1complete", typeof(DateTime));
        table.Columns.Add("exam2date", typeof(DateTime));
        table.Columns.Add("exam2complete", typeof(DateTime));
        table.Columns.Add("exam3date", typeof(DateTime));
        table.Columns.Add("exam3complete", typeof(DateTime));

        table.Rows.Add("joe", "2018-01-30", DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue);
        table.Rows.Add("james", "2018-02-14", "2018-02-21", "2018-03-02", DateTime.MinValue, DateTime.MinValue, DateTime.MinValue);
        table.Rows.Add("javier", "2018-01-01", "2018-01-14", "2018-03-01", "2018-03-12", "2018-04-01", DateTime.MinValue);
        return table;
    }
}