Get a Maximum Date value from X tables in DataSet












7














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.










share|improve this question





























    7














    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.










    share|improve this question



























      7












      7








      7


      1





      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.










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 16 hours ago









      Uwe Keim

      27.4k30128210




      27.4k30128210










      asked 16 hours ago









      Dor Lugasi

      27214




      27214
























          1 Answer
          1






          active

          oldest

          votes


















          5














          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.Range to generate the indices enabling us to access each individual DataTable.

          • Uses SelectMany to get a single IEnumerable<DataRow>

          • Uses Max to 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.Range to generate the indices enabling us to access each individual DataTable.

          • Uses Select to get an IEnumerable<DateTime> consisting of the maximum DateTime of each DataTable

          • Uses Max to get the maximum value of the of the DateTime's and then convert to a string representation.






          share|improve this answer



















          • 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











          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          5














          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.Range to generate the indices enabling us to access each individual DataTable.

          • Uses SelectMany to get a single IEnumerable<DataRow>

          • Uses Max to 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.Range to generate the indices enabling us to access each individual DataTable.

          • Uses Select to get an IEnumerable<DateTime> consisting of the maximum DateTime of each DataTable

          • Uses Max to get the maximum value of the of the DateTime's and then convert to a string representation.






          share|improve this answer



















          • 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
















          5














          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.Range to generate the indices enabling us to access each individual DataTable.

          • Uses SelectMany to get a single IEnumerable<DataRow>

          • Uses Max to 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.Range to generate the indices enabling us to access each individual DataTable.

          • Uses Select to get an IEnumerable<DateTime> consisting of the maximum DateTime of each DataTable

          • Uses Max to get the maximum value of the of the DateTime's and then convert to a string representation.






          share|improve this answer



















          • 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














          5












          5








          5






          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.Range to generate the indices enabling us to access each individual DataTable.

          • Uses SelectMany to get a single IEnumerable<DataRow>

          • Uses Max to 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.Range to generate the indices enabling us to access each individual DataTable.

          • Uses Select to get an IEnumerable<DateTime> consisting of the maximum DateTime of each DataTable

          • Uses Max to get the maximum value of the of the DateTime's and then convert to a string representation.






          share|improve this answer














          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.Range to generate the indices enabling us to access each individual DataTable.

          • Uses SelectMany to get a single IEnumerable<DataRow>

          • Uses Max to 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.Range to generate the indices enabling us to access each individual DataTable.

          • Uses Select to get an IEnumerable<DateTime> consisting of the maximum DateTime of each DataTable

          • Uses Max to get the maximum value of the of the DateTime's and then convert to a string representation.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          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














          • 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


















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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