Get a Maximum Date value from X tables in DataSet
I have a DataSet with 1-3 tables, each table have a column named m_date (string)
I want to get max value from all of them using LINQ.
I know how to get each table Maximum value:
var maxDate=ds.Tables[index].AsEnumerable()
.Max(x=>DateTime.Parse(x["m_date"].ToString())).ToString();
but I don't know how to get Max value from all the tables information
Edit:
I now have something like this which works:
DateTime maxDate=DateTime.MinValue;
foreach (DataTable tbl in ds.Tables)
{
DateTime maxDateCur=ds.Tables[index].AsEnumerable()
.Max(x=>DateTime.Parse(x["m_date"].ToString()));
maxDate=new DateTime {maxDateCur,maxDate}.Max();
}
but I have a feeling it could be done better.
c# linq datatable
add a comment |
I have a DataSet with 1-3 tables, each table have a column named m_date (string)
I want to get max value from all of them using LINQ.
I know how to get each table Maximum value:
var maxDate=ds.Tables[index].AsEnumerable()
.Max(x=>DateTime.Parse(x["m_date"].ToString())).ToString();
but I don't know how to get Max value from all the tables information
Edit:
I now have something like this which works:
DateTime maxDate=DateTime.MinValue;
foreach (DataTable tbl in ds.Tables)
{
DateTime maxDateCur=ds.Tables[index].AsEnumerable()
.Max(x=>DateTime.Parse(x["m_date"].ToString()));
maxDate=new DateTime {maxDateCur,maxDate}.Max();
}
but I have a feeling it could be done better.
c# linq datatable
add a comment |
I have a DataSet with 1-3 tables, each table have a column named m_date (string)
I want to get max value from all of them using LINQ.
I know how to get each table Maximum value:
var maxDate=ds.Tables[index].AsEnumerable()
.Max(x=>DateTime.Parse(x["m_date"].ToString())).ToString();
but I don't know how to get Max value from all the tables information
Edit:
I now have something like this which works:
DateTime maxDate=DateTime.MinValue;
foreach (DataTable tbl in ds.Tables)
{
DateTime maxDateCur=ds.Tables[index].AsEnumerable()
.Max(x=>DateTime.Parse(x["m_date"].ToString()));
maxDate=new DateTime {maxDateCur,maxDate}.Max();
}
but I have a feeling it could be done better.
c# linq datatable
I have a DataSet with 1-3 tables, each table have a column named m_date (string)
I want to get max value from all of them using LINQ.
I know how to get each table Maximum value:
var maxDate=ds.Tables[index].AsEnumerable()
.Max(x=>DateTime.Parse(x["m_date"].ToString())).ToString();
but I don't know how to get Max value from all the tables information
Edit:
I now have something like this which works:
DateTime maxDate=DateTime.MinValue;
foreach (DataTable tbl in ds.Tables)
{
DateTime maxDateCur=ds.Tables[index].AsEnumerable()
.Max(x=>DateTime.Parse(x["m_date"].ToString()));
maxDate=new DateTime {maxDateCur,maxDate}.Max();
}
but I have a feeling it could be done better.
c# linq datatable
c# linq datatable
edited 16 hours ago
Uwe Keim
27.4k30128210
27.4k30128210
asked 16 hours ago
Dor Lugasi
27214
27214
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You could do it as:
var maxDate = Enumerable.Range(0, ds.Tables.Count)
.SelectMany(index => ds.Tables[index].AsEnumerable())
.Max(dataRow => dataRow.Field<DateTime>("m_date"))
.ToString();
- Uses
Enumerable.Rangeto generate the indices enabling us to access each individualDataTable. - Uses
SelectManyto get a singleIEnumerable<DataRow>
- Uses
Maxto get the maximum value and convert it to a string.
Slightly different variant would be:
var maxDate = Enumerable.Range(0, ds.Tables.Count)
.Select(index =>
ds.Tables[index].AsEnumerable()
.Max(dataRow => dataRow.Field<DateTime>("m_date")))
.Max().ToString();
- Uses
Enumerable.Rangeto generate the indices enabling us to access each individualDataTable. - Uses
Selectto get anIEnumerable<DateTime>consisting of the maximumDateTimeof eachDataTable
- Uses
Maxto get the maximum value of the of the DateTime's and then convert to a string representation.
1
Works Great, the only change I made is changing the field to DateTime.ParseExact, because my datetime string is dd/mm/yyyy
– Dor Lugasi
15 hours ago
and adding .Where(x=>!string.IsNullOrEmpty(x.Field<string>("col_name")))
– Dor Lugasi
14 hours ago
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%2f53942882%2fget-a-maximum-date-value-from-x-tables-in-dataset%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
You could do it as:
var maxDate = Enumerable.Range(0, ds.Tables.Count)
.SelectMany(index => ds.Tables[index].AsEnumerable())
.Max(dataRow => dataRow.Field<DateTime>("m_date"))
.ToString();
- Uses
Enumerable.Rangeto generate the indices enabling us to access each individualDataTable. - Uses
SelectManyto get a singleIEnumerable<DataRow>
- Uses
Maxto get the maximum value and convert it to a string.
Slightly different variant would be:
var maxDate = Enumerable.Range(0, ds.Tables.Count)
.Select(index =>
ds.Tables[index].AsEnumerable()
.Max(dataRow => dataRow.Field<DateTime>("m_date")))
.Max().ToString();
- Uses
Enumerable.Rangeto generate the indices enabling us to access each individualDataTable. - Uses
Selectto get anIEnumerable<DateTime>consisting of the maximumDateTimeof eachDataTable
- Uses
Maxto get the maximum value of the of the DateTime's and then convert to a string representation.
1
Works Great, the only change I made is changing the field to DateTime.ParseExact, because my datetime string is dd/mm/yyyy
– Dor Lugasi
15 hours ago
and adding .Where(x=>!string.IsNullOrEmpty(x.Field<string>("col_name")))
– Dor Lugasi
14 hours ago
add a comment |
You could do it as:
var maxDate = Enumerable.Range(0, ds.Tables.Count)
.SelectMany(index => ds.Tables[index].AsEnumerable())
.Max(dataRow => dataRow.Field<DateTime>("m_date"))
.ToString();
- Uses
Enumerable.Rangeto generate the indices enabling us to access each individualDataTable. - Uses
SelectManyto get a singleIEnumerable<DataRow>
- Uses
Maxto get the maximum value and convert it to a string.
Slightly different variant would be:
var maxDate = Enumerable.Range(0, ds.Tables.Count)
.Select(index =>
ds.Tables[index].AsEnumerable()
.Max(dataRow => dataRow.Field<DateTime>("m_date")))
.Max().ToString();
- Uses
Enumerable.Rangeto generate the indices enabling us to access each individualDataTable. - Uses
Selectto get anIEnumerable<DateTime>consisting of the maximumDateTimeof eachDataTable
- Uses
Maxto get the maximum value of the of the DateTime's and then convert to a string representation.
1
Works Great, the only change I made is changing the field to DateTime.ParseExact, because my datetime string is dd/mm/yyyy
– Dor Lugasi
15 hours ago
and adding .Where(x=>!string.IsNullOrEmpty(x.Field<string>("col_name")))
– Dor Lugasi
14 hours ago
add a comment |
You could do it as:
var maxDate = Enumerable.Range(0, ds.Tables.Count)
.SelectMany(index => ds.Tables[index].AsEnumerable())
.Max(dataRow => dataRow.Field<DateTime>("m_date"))
.ToString();
- Uses
Enumerable.Rangeto generate the indices enabling us to access each individualDataTable. - Uses
SelectManyto get a singleIEnumerable<DataRow>
- Uses
Maxto get the maximum value and convert it to a string.
Slightly different variant would be:
var maxDate = Enumerable.Range(0, ds.Tables.Count)
.Select(index =>
ds.Tables[index].AsEnumerable()
.Max(dataRow => dataRow.Field<DateTime>("m_date")))
.Max().ToString();
- Uses
Enumerable.Rangeto generate the indices enabling us to access each individualDataTable. - Uses
Selectto get anIEnumerable<DateTime>consisting of the maximumDateTimeof eachDataTable
- Uses
Maxto get the maximum value of the of the DateTime's and then convert to a string representation.
You could do it as:
var maxDate = Enumerable.Range(0, ds.Tables.Count)
.SelectMany(index => ds.Tables[index].AsEnumerable())
.Max(dataRow => dataRow.Field<DateTime>("m_date"))
.ToString();
- Uses
Enumerable.Rangeto generate the indices enabling us to access each individualDataTable. - Uses
SelectManyto get a singleIEnumerable<DataRow>
- Uses
Maxto get the maximum value and convert it to a string.
Slightly different variant would be:
var maxDate = Enumerable.Range(0, ds.Tables.Count)
.Select(index =>
ds.Tables[index].AsEnumerable()
.Max(dataRow => dataRow.Field<DateTime>("m_date")))
.Max().ToString();
- Uses
Enumerable.Rangeto generate the indices enabling us to access each individualDataTable. - Uses
Selectto get anIEnumerable<DateTime>consisting of the maximumDateTimeof eachDataTable
- Uses
Maxto get the maximum value of the of the DateTime's and then convert to a string representation.
edited 15 hours ago
answered 16 hours ago
Aomine
39k73366
39k73366
1
Works Great, the only change I made is changing the field to DateTime.ParseExact, because my datetime string is dd/mm/yyyy
– Dor Lugasi
15 hours ago
and adding .Where(x=>!string.IsNullOrEmpty(x.Field<string>("col_name")))
– Dor Lugasi
14 hours ago
add a comment |
1
Works Great, the only change I made is changing the field to DateTime.ParseExact, because my datetime string is dd/mm/yyyy
– Dor Lugasi
15 hours ago
and adding .Where(x=>!string.IsNullOrEmpty(x.Field<string>("col_name")))
– Dor Lugasi
14 hours ago
1
1
Works Great, the only change I made is changing the field to DateTime.ParseExact, because my datetime string is dd/mm/yyyy
– Dor Lugasi
15 hours ago
Works Great, the only change I made is changing the field to DateTime.ParseExact, because my datetime string is dd/mm/yyyy
– Dor Lugasi
15 hours ago
and adding .Where(x=>!string.IsNullOrEmpty(x.Field<string>("col_name")))
– Dor Lugasi
14 hours ago
and adding .Where(x=>!string.IsNullOrEmpty(x.Field<string>("col_name")))
– Dor Lugasi
14 hours ago
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53942882%2fget-a-maximum-date-value-from-x-tables-in-dataset%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