none
select dintinct years in a DateTime field RRS feed

  • Question

  • Hi!

    I have a DataTable with a column of different dates (formatted as strings). I would like to select all distinct years present in the column. Is that possible in LINQ?

    Best regards Tobias

    Sunday, July 11, 2010 8:27 AM

Answers

  • Hi Anonymous3532,

    You just need to mofify BeharaVenkata's code a little. For example:

    DataTable dt = new DataTable();
    dt.Columns.Add(new DataColumn("Date", typeof(string)));
    DataRow dr = dt.NewRow();
    dr["Date"] = "2010-01-20";
    dt.Rows.Add(dr);
    
    DataRow dr1 = dt.NewRow();
    dr1["Date"] = "2011-02-20";
    dt.Rows.Add(dr1);
    
    DataRow dr2 = dt.NewRow();
    dr2["Date"] = "2010-01-20";
    dt.Rows.Add(dr2);
    
    DataRow dr3 = dt.NewRow();
    dr3["Date"] = "2012-03-12";
    dt.Rows.Add(dr3);
    
    DataRow dr4 = dt.NewRow();
    dr4["Date"] = "2012-01-2";
    dt.Rows.Add(dr4);
    
    DataRow dr5 = dt.NewRow();
    dr5["Date"] = "2013-01-20";
    dt.Rows.Add(dr5);
    
    var dataRows = (from r in dt.AsEnumerable()
            select DateTime.Parse(r.Field<string>("Date")).Year).Distinct();
    
    foreach (var item in dataRows)
      Console.WriteLine(item);
    
    

    Best regards,
    Alex Liang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Anonymous3532 Tuesday, July 13, 2010 6:51 AM
    Tuesday, July 13, 2010 6:34 AM
    Moderator

All replies

  • DataTable dt = new DataTable();
          dt.Columns.Add(new DataColumn("Date",typeof(string)));
          DataRow dr = dt.NewRow();
          dr["Date"] = "2010-01-20";
          dt.Rows.Add(dr);
    
          DataRow dr1 = dt.NewRow();
          dr1["Date"] = "2010-02-20";
          dt.Rows.Add(dr1);
    
          DataRow dr2 = dt.NewRow();
          dr2["Date"] = "2010-01-20";
          dt.Rows.Add(dr2);
    
          DataRow dr3 = dt.NewRow();
          dr3["Date"] = "2010-03-12";
          dt.Rows.Add(dr3);
    
          DataRow dr4 = dt.NewRow();
          dr4["Date"] = "2010-01-2";
          dt.Rows.Add(dr4);
    
          DataRow dr5 = dt.NewRow();
          dr5["Date"] = "2010-01-20";
          dt.Rows.Add(dr5);
    
          var dataRows = (from r in dt.AsEnumerable()
                  select r.Field<string>("Date")).Distinct();
    
          foreach (var item in dataRows)
            Console.WriteLine(item);

    Sunday, July 11, 2010 9:23 AM
  • Thanks, but what if I only want to get the distinct years. The code provided only returns the distinct dates.
    Sunday, July 11, 2010 9:52 AM
  • Hi Anonymous3532,

    You just need to mofify BeharaVenkata's code a little. For example:

    DataTable dt = new DataTable();
    dt.Columns.Add(new DataColumn("Date", typeof(string)));
    DataRow dr = dt.NewRow();
    dr["Date"] = "2010-01-20";
    dt.Rows.Add(dr);
    
    DataRow dr1 = dt.NewRow();
    dr1["Date"] = "2011-02-20";
    dt.Rows.Add(dr1);
    
    DataRow dr2 = dt.NewRow();
    dr2["Date"] = "2010-01-20";
    dt.Rows.Add(dr2);
    
    DataRow dr3 = dt.NewRow();
    dr3["Date"] = "2012-03-12";
    dt.Rows.Add(dr3);
    
    DataRow dr4 = dt.NewRow();
    dr4["Date"] = "2012-01-2";
    dt.Rows.Add(dr4);
    
    DataRow dr5 = dt.NewRow();
    dr5["Date"] = "2013-01-20";
    dt.Rows.Add(dr5);
    
    var dataRows = (from r in dt.AsEnumerable()
            select DateTime.Parse(r.Field<string>("Date")).Year).Distinct();
    
    foreach (var item in dataRows)
      Console.WriteLine(item);
    
    

    Best regards,
    Alex Liang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Anonymous3532 Tuesday, July 13, 2010 6:51 AM
    Tuesday, July 13, 2010 6:34 AM
    Moderator
  • I see! That solved my problem!

    Thank you :)

    Tuesday, July 13, 2010 6:51 AM