Run SQL Query Against DataTable The Next CEO of Stack OverflowLINQ query on a DataTableLINQ query on a DataTableHow can I prevent SQL injection in PHP?How do I perform an IF…THEN in an SQL SELECT?Add a column with a default value to an existing table in SQL ServerHow to return only the Date from a SQL Server DateTime datatypeHow to concatenate text from multiple rows into a single text string in SQL server?Inserting multiple rows in a single SQL query?How do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL tableHow to import an SQL file using the command line in MySQL?
Is a stroke of luck acceptable after a series of unfavorable events?
Anatomically Correct Strange Women In Ponds Distributing Swords
% symbol leads to superlong (forever?) compilations
Whats the best way to handle refactoring a big file?
Increase performance creating Mandelbrot set in python
Go Pregnant or Go Home
How can I quit an app using Terminal?
How do I get the green key off the shelf in the Dobby level of Lego Harry Potter 2?
What can we do to stop prior company from asking us questions?
Would this house-rule that treats advantage as a +1 to the roll instead (and disadvantage as -1) and allows them to stack be balanced?
WOW air has ceased operation, can I get my tickets refunded?
Shade part of a Venn diagram
MAZDA 3 2006 (UK) - poor acceleration then takes off at 3250 revs
Horror movie/show or scene where a horse creature opens its mouth really wide and devours a man in a stables
When Does an Atlas Uniquely Define a Manifold?
How should I support this large drywall patch?
Is it my responsibility to learn a new technology in my own time my employer wants to implement?
What is the point of a new vote on May's deal when the indicative votes suggest she will not win?
How does practicing restraint and performing actions of merit purify the mind?
Is it safe to use c_str() on a temporary string?
Does it take more energy to get to Venus or to Mars?
How to write the block matrix in LaTex?
Natural language into sentence logic
Only print output after finding pattern
Run SQL Query Against DataTable
The Next CEO of Stack OverflowLINQ query on a DataTableLINQ query on a DataTableHow can I prevent SQL injection in PHP?How do I perform an IF…THEN in an SQL SELECT?Add a column with a default value to an existing table in SQL ServerHow to return only the Date from a SQL Server DateTime datatypeHow to concatenate text from multiple rows into a single text string in SQL server?Inserting multiple rows in a single SQL query?How do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL tableHow to import an SQL file using the command line in MySQL?
I am populating a DataTable like this
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();
How can I run this SQL statement against the DataTable to get the data from the datatable in a format I can use?
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;
c# sql winforms linq datatable
add a comment |
I am populating a DataTable like this
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();
How can I run this SQL statement against the DataTable to get the data from the datatable in a format I can use?
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;
c# sql winforms linq datatable
1
Use table.AsEnumerable() to enumerate through table.
– jdweng
Mar 8 at 13:19
1
May be worth checking intoLINQ
instead ofSQL
. They're practically the same thoughLINQ
is within C#. It will allow you pretty much query your dataTable just like aSQL query
. Linq Query On A DataTable
– Symon
Mar 8 at 13:28
1
you can't run plain SQL against a data table. you'd have to iterate through the rows performing logic, or use LINQ like Symon suggests.
– Jeremy
Mar 8 at 14:01
add a comment |
I am populating a DataTable like this
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();
How can I run this SQL statement against the DataTable to get the data from the datatable in a format I can use?
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;
c# sql winforms linq datatable
I am populating a DataTable like this
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();
How can I run this SQL statement against the DataTable to get the data from the datatable in a format I can use?
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;
c# sql winforms linq datatable
c# sql winforms linq datatable
edited Mar 8 at 13:40
Ajanyan Pradeep
288111
288111
asked Mar 8 at 13:12
Doctor FordDoctor Ford
828
828
1
Use table.AsEnumerable() to enumerate through table.
– jdweng
Mar 8 at 13:19
1
May be worth checking intoLINQ
instead ofSQL
. They're practically the same thoughLINQ
is within C#. It will allow you pretty much query your dataTable just like aSQL query
. Linq Query On A DataTable
– Symon
Mar 8 at 13:28
1
you can't run plain SQL against a data table. you'd have to iterate through the rows performing logic, or use LINQ like Symon suggests.
– Jeremy
Mar 8 at 14:01
add a comment |
1
Use table.AsEnumerable() to enumerate through table.
– jdweng
Mar 8 at 13:19
1
May be worth checking intoLINQ
instead ofSQL
. They're practically the same thoughLINQ
is within C#. It will allow you pretty much query your dataTable just like aSQL query
. Linq Query On A DataTable
– Symon
Mar 8 at 13:28
1
you can't run plain SQL against a data table. you'd have to iterate through the rows performing logic, or use LINQ like Symon suggests.
– Jeremy
Mar 8 at 14:01
1
1
Use table.AsEnumerable() to enumerate through table.
– jdweng
Mar 8 at 13:19
Use table.AsEnumerable() to enumerate through table.
– jdweng
Mar 8 at 13:19
1
1
May be worth checking into
LINQ
instead of SQL
. They're practically the same though LINQ
is within C#. It will allow you pretty much query your dataTable just like a SQL query
. Linq Query On A DataTable– Symon
Mar 8 at 13:28
May be worth checking into
LINQ
instead of SQL
. They're practically the same though LINQ
is within C#. It will allow you pretty much query your dataTable just like a SQL query
. Linq Query On A DataTable– Symon
Mar 8 at 13:28
1
1
you can't run plain SQL against a data table. you'd have to iterate through the rows performing logic, or use LINQ like Symon suggests.
– Jeremy
Mar 8 at 14:01
you can't run plain SQL against a data table. you'd have to iterate through the rows performing logic, or use LINQ like Symon suggests.
– Jeremy
Mar 8 at 14:01
add a comment |
1 Answer
1
active
oldest
votes
It looks like a bit wierd but if you change the null's to DateTime.MinValue below code seems to work...
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;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55063951%2frun-sql-query-against-datatable%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
It looks like a bit wierd but if you change the null's to DateTime.MinValue below code seems to work...
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;
add a comment |
It looks like a bit wierd but if you change the null's to DateTime.MinValue below code seems to work...
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;
add a comment |
It looks like a bit wierd but if you change the null's to DateTime.MinValue below code seems to work...
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;
It looks like a bit wierd but if you change the null's to DateTime.MinValue below code seems to work...
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;
answered Mar 8 at 14:19
emumcuemumcu
438
438
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55063951%2frun-sql-query-against-datatable%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Use table.AsEnumerable() to enumerate through table.
– jdweng
Mar 8 at 13:19
1
May be worth checking into
LINQ
instead ofSQL
. They're practically the same thoughLINQ
is within C#. It will allow you pretty much query your dataTable just like aSQL query
. Linq Query On A DataTable– Symon
Mar 8 at 13:28
1
you can't run plain SQL against a data table. you'd have to iterate through the rows performing logic, or use LINQ like Symon suggests.
– Jeremy
Mar 8 at 14:01