Выполнить SQL-запрос к DataTable - PullRequest
0 голосов
/ 08 марта 2019

Я заполняю 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();
}

Как выполнить этот оператор 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 голосов
/ 08 марта 2019

Это выглядит немного странно, но если вы измените значение 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;
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...