Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. Converting Long Date To Short Date Format in Linq Query

Converting Long Date To Short Date Format in Linq Query

Scheduled Pinned Locked Moved LINQ
helpcsharpdatabaselinq
22 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Sanket Patil

    Hi To All, Well, I am using Linq in my project. Using Linq query I am concatenating two dates to show financial year in ComboBox. The Query I am Using is :- "(from n in logUserDc.mas_COMPANies where n.CM_COMP_ID == 1 select new { Sdate = "From :" + n.CM_ST_DATE + " - To " + n.CM_END_DATE, Code = n.CM_CODE });". This Query is working fine, But the result that i wish is not getting. I want the result as " From : 01/Apr/2009 - T0 : 31/Mar/2009" whereas i am getting the result is :- " From : Apr 1 2009 12:00AM - To : Mar 31 2010 12:00AM " Which i dont want. Please Help me to solve my problem. - -- ---- Thanks & Regards Sanket Patil.

    .

    J Offline
    J Offline
    J4amieC
    wrote on last edited by
    #7

    assuming n.CM_ST_DATE and n.CM_END_DATE are actual DateTime instances and not already strings:

    (from n in logUserDc.mas_COMPANies where n.CM_COMP_ID == 1
    select new { Sdate = "From :" + n.CM_ST_DATE.ToString("MMM dd yyyy") + " - To " + n.CM_END_DATE.ToString("MMM dd yyyy"), Code = n.CM_CODE })

    S 1 Reply Last reply
    0
    • J J4amieC

      assuming n.CM_ST_DATE and n.CM_END_DATE are actual DateTime instances and not already strings:

      (from n in logUserDc.mas_COMPANies where n.CM_COMP_ID == 1
      select new { Sdate = "From :" + n.CM_ST_DATE.ToString("MMM dd yyyy") + " - To " + n.CM_END_DATE.ToString("MMM dd yyyy"), Code = n.CM_CODE })

      S Offline
      S Offline
      Sanket Patil
      wrote on last edited by
      #8

      Hi J4amieC, Thanks For Your Help. But it Doesn't work. It Shows me the Error As Follow: " Method 'System.String ToString(System.String)' has no supported translation to SQL." So If There is any other method For This Please Reply. Thanks a lot For Your Reply Thanks & Regards -- --- ---- Sanket Patil

      .

      J L 2 Replies Last reply
      0
      • L Lost User

        Sanket.Patil wrote:

        So is it possible to convert the query result to the result that i want before providing displaymember to ComboBox.

        My apologies, but I misread your original question, I assumed that you were adding DateTime objects to the ComboBox. In the statement

        "(from n in logUserDc.mas_COMPANies where n.CM_COMP_ID == 1
        select new { Sdate = "From :" + n.CM_ST_DATE + " - To " + n.CM_END_DATE, Code = n.CM_CODE });".

        you need to format the two dates n.CM_ST_DATE and n.CM_END_DATE. Using String.Format() may be the better option than just concatenating from the ToString() methods.

        S Offline
        S Offline
        Sanket Patil
        wrote on last edited by
        #9

        Hi Richard, Thanks For Your Reply. But It doesn't work. It shows me the error as follows : " Could not translate expression 'Format("dd/MMM/yyyy", Convert(n.CM_END_DATE))' into SQL and could not treat it as a local expression." Please Reply me if you have any other option. Thanks once again for your reply. Thanks & Regards -- --- ---- Sanket Patil

        .

        1 Reply Last reply
        0
        • S Sanket Patil

          Hi J4amieC, Thanks For Your Help. But it Doesn't work. It Shows me the Error As Follow: " Method 'System.String ToString(System.String)' has no supported translation to SQL." So If There is any other method For This Please Reply. Thanks a lot For Your Reply Thanks & Regards -- --- ---- Sanket Patil

          .

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #10

          Sanket.Patil wrote:

          So If There is any other method For This Please Reply.

          Make sure those 2 fields are of type DATETIME when doing your SQL select.

          S 1 Reply Last reply
          0
          • S Sanket Patil

            Hi J4amieC, Thanks For Your Help. But it Doesn't work. It Shows me the Error As Follow: " Method 'System.String ToString(System.String)' has no supported translation to SQL." So If There is any other method For This Please Reply. Thanks a lot For Your Reply Thanks & Regards -- --- ---- Sanket Patil

            .

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #11

            Sanket.Patil wrote:

            So If There is any other method For This Please Reply.

            Please post your code (between <pre></pre> tags) to show what you have written, we cannot verify things just from an error message.

            S 1 Reply Last reply
            0
            • L Lost User

              Sanket.Patil wrote:

              So If There is any other method For This Please Reply.

              Please post your code (between <pre></pre> tags) to show what you have written, we cannot verify things just from an error message.

              S Offline
              S Offline
              Sanket Patil
              wrote on last edited by
              #12

              Hi Richard, Here is my Code to Review :-

              private void cmbCompanyName_SelectedIndexChanged(object sender, EventArgs e)
              {
              try
              {
              cmbFinacialYear.DataSource = null;
              cmbFinacialYear.DataSource = (from n in logUserDc.mas_COMPANies where n.CM_COMP_ID == Convert.ToInt32(cmbCompanyName.SelectedValue) orderby n.CM_CODE descending select new { Sdate = " From : " + n.CM_ST_DATE + " - To : " + n.CM_END_DATE, Code = n.CM_CODE });
              cmbFinacialYear.DisplayMember = "Sdate";
              cmbFinacialYear.ValueMember = "Code";
              cmbFinacialYear.SelectedIndex = -1;
              }
              catch (Exception ex)
              {
              objcon.WriteLog("Login Master", "btnLogin_Click", ex);
              }
              }

              Hope You will get better Solution From These. Thanks For Help..... Thanks & Regard -- --- ---- Sanket Patil

              .

              L 1 Reply Last reply
              0
              • J J4amieC

                Sanket.Patil wrote:

                So If There is any other method For This Please Reply.

                Make sure those 2 fields are of type DATETIME when doing your SQL select.

                S Offline
                S Offline
                Sanket Patil
                wrote on last edited by
                #13

                Hi J4amieC, Here is my code to review :-

                private void cmbCompanyName_SelectedIndexChanged(object sender, EventArgs e)
                {
                try
                {
                cmbFinacialYear.DataSource = null;
                cmbFinacialYear.DataSource = (from n in logUserDc.mas_COMPANies where n.CM_COMP_ID == Convert.ToInt32(cmbCompanyName.SelectedValue) orderby n.CM_CODE descending select new { Sdate = " From : " + n.CM_ST_DATE + " - To : " + n.CM_END_DATE, Code = n.CM_CODE });
                cmbFinacialYear.DisplayMember = "Sdate";
                cmbFinacialYear.ValueMember = "Code";
                cmbFinacialYear.SelectedIndex = -1;
                }
                catch (Exception ex)
                {
                objcon.WriteLog("Login Master", "btnLogin_Click", ex);
                }
                }

                Hope you will find the better Solution From these. Thanks for the Help.... Thanks & Regards -- --- ---- Sanket Patil.

                .

                J 1 Reply Last reply
                0
                • S Sanket Patil

                  Hi Richard, Here is my Code to Review :-

                  private void cmbCompanyName_SelectedIndexChanged(object sender, EventArgs e)
                  {
                  try
                  {
                  cmbFinacialYear.DataSource = null;
                  cmbFinacialYear.DataSource = (from n in logUserDc.mas_COMPANies where n.CM_COMP_ID == Convert.ToInt32(cmbCompanyName.SelectedValue) orderby n.CM_CODE descending select new { Sdate = " From : " + n.CM_ST_DATE + " - To : " + n.CM_END_DATE, Code = n.CM_CODE });
                  cmbFinacialYear.DisplayMember = "Sdate";
                  cmbFinacialYear.ValueMember = "Code";
                  cmbFinacialYear.SelectedIndex = -1;
                  }
                  catch (Exception ex)
                  {
                  objcon.WriteLog("Login Master", "btnLogin_Click", ex);
                  }
                  }

                  Hope You will get better Solution From These. Thanks For Help..... Thanks & Regard -- --- ---- Sanket Patil

                  .

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #14

                  You have posted the original code twice and in both cases you are creating Sdate as a concatenation of various fields including n.CM_ST_DATE. However this latter field will be returning its default ToString() method rather than the formatted version that you require. Try n.CM_ST_DATE.ToString("D"), or some alternate DateTime format string to get the date only. [EDIT]above should read n.CM_ST_DATE.ToString("d").[/EDIT]

                  modified on Tuesday, October 6, 2009 8:41 AM

                  S 1 Reply Last reply
                  0
                  • L Lost User

                    You have posted the original code twice and in both cases you are creating Sdate as a concatenation of various fields including n.CM_ST_DATE. However this latter field will be returning its default ToString() method rather than the formatted version that you require. Try n.CM_ST_DATE.ToString("D"), or some alternate DateTime format string to get the date only. [EDIT]above should read n.CM_ST_DATE.ToString("d").[/EDIT]

                    modified on Tuesday, October 6, 2009 8:41 AM

                    S Offline
                    S Offline
                    Sanket Patil
                    wrote on last edited by
                    #15

                    Hi Richard, Thanks a lot for your help. The good news is i found the solution but not satisfied. Still the solution for now is best.Instead of getting result in "dd/MMM/yyyy" i am getting the result as "d/MM/yyyy". The Code I used for this is as Follows:- Previous Code :-

                    private void cmbCompanyName_SelectedIndexChanged(object sender, EventArgs e)
                    {
                    try
                    {
                    cmbFinacialYear.DataSource = null;
                    cmbFinacialYear.DataSource = (from n in logUserDc.mas_COMPANies where n.CM_COMP_ID == Convert.ToInt32(cmbCompanyName.SelectedValue) orderby n.CM_CODE descending select new { Sdate = " From : " + n.CM_ST_DATE + " - To : " + n.CM_END_DATE, Code = n.CM_CODE });
                    cmbFinacialYear.DisplayMember = "Sdate";
                    cmbFinacialYear.ValueMember = "Code";
                    cmbFinacialYear.SelectedIndex = -1;
                    }
                    catch (Exception ex)
                    {
                    objcon.WriteLog("Login Master", "btnLogin_Click", ex);
                    }
                    }

                    And The Code I Made Changes is :-

                    private void cmbCompanyName_SelectedIndexChanged(object sender, EventArgs e)
                    {
                    try
                    {
                    cmbFinacialYear.DataSource = null;
                    cmbFinacialYear.DataSource = (from n in logUserDc.mas_COMPANies where n.CM_COMP_ID == Convert.ToInt32(cmbCompanyName.SelectedValue) orderby n.CM_CODE descending select new { Sdate = " From : " + n.CM_ST_DATE.Day + "/" + n.CM_ST_DATE.Month + "/" + n.CM_ST_DATE.Year + " - To : " + n.CM_END_DATE.Day + "/" + n.CM_END_DATE.Month + "/" + n.CM_END_DATE.Year, Code = n.CM_CODE });
                    cmbFinacialYear.DisplayMember = "Sdate";
                    cmbFinacialYear.ValueMember = "Code";
                    cmbFinacialYear.SelectedIndex = -1;
                    }
                    catch (Exception ex)
                    {
                    objcon.WriteLog("Login Master", "btnLogin_Click", ex);
                    }
                    }

                    Hope You will be satisfy From this. Thanks For Help Thanks & Regards -- --- ---- Sanket. Patil

                    .

                    L 1 Reply Last reply
                    0
                    • S Sanket Patil

                      Hi Richard, Thanks a lot for your help. The good news is i found the solution but not satisfied. Still the solution for now is best.Instead of getting result in "dd/MMM/yyyy" i am getting the result as "d/MM/yyyy". The Code I used for this is as Follows:- Previous Code :-

                      private void cmbCompanyName_SelectedIndexChanged(object sender, EventArgs e)
                      {
                      try
                      {
                      cmbFinacialYear.DataSource = null;
                      cmbFinacialYear.DataSource = (from n in logUserDc.mas_COMPANies where n.CM_COMP_ID == Convert.ToInt32(cmbCompanyName.SelectedValue) orderby n.CM_CODE descending select new { Sdate = " From : " + n.CM_ST_DATE + " - To : " + n.CM_END_DATE, Code = n.CM_CODE });
                      cmbFinacialYear.DisplayMember = "Sdate";
                      cmbFinacialYear.ValueMember = "Code";
                      cmbFinacialYear.SelectedIndex = -1;
                      }
                      catch (Exception ex)
                      {
                      objcon.WriteLog("Login Master", "btnLogin_Click", ex);
                      }
                      }

                      And The Code I Made Changes is :-

                      private void cmbCompanyName_SelectedIndexChanged(object sender, EventArgs e)
                      {
                      try
                      {
                      cmbFinacialYear.DataSource = null;
                      cmbFinacialYear.DataSource = (from n in logUserDc.mas_COMPANies where n.CM_COMP_ID == Convert.ToInt32(cmbCompanyName.SelectedValue) orderby n.CM_CODE descending select new { Sdate = " From : " + n.CM_ST_DATE.Day + "/" + n.CM_ST_DATE.Month + "/" + n.CM_ST_DATE.Year + " - To : " + n.CM_END_DATE.Day + "/" + n.CM_END_DATE.Month + "/" + n.CM_END_DATE.Year, Code = n.CM_CODE });
                      cmbFinacialYear.DisplayMember = "Sdate";
                      cmbFinacialYear.ValueMember = "Code";
                      cmbFinacialYear.SelectedIndex = -1;
                      }
                      catch (Exception ex)
                      {
                      objcon.WriteLog("Login Master", "btnLogin_Click", ex);
                      }
                      }

                      Hope You will be satisfy From this. Thanks For Help Thanks & Regards -- --- ---- Sanket. Patil

                      .

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #16

                      Well I don't know why you are making things more difficult for yourself. I suggested that you use n.CM_ST_DATE.ToString(formatstring) (where formatstring is a standard or custom date format string), which will provide everything and anything you need. Take a look at the documentation here on MSDN[^] for all the possibilities.

                      J 1 Reply Last reply
                      0
                      • L Lost User

                        Well I don't know why you are making things more difficult for yourself. I suggested that you use n.CM_ST_DATE.ToString(formatstring) (where formatstring is a standard or custom date format string), which will provide everything and anything you need. Take a look at the documentation here on MSDN[^] for all the possibilities.

                        J Offline
                        J Offline
                        J4amieC
                        wrote on last edited by
                        #17

                        Richard MacCutchan wrote:

                        I suggested that you use n.CM_ST_DATE.ToString(formatstring)

                        As did I, more than 24 hours ago. This person is incapable of reading documentation or taking advice.. therefore I gave up. I suggest you do too.

                        L 1 Reply Last reply
                        0
                        • J J4amieC

                          Richard MacCutchan wrote:

                          I suggested that you use n.CM_ST_DATE.ToString(formatstring)

                          As did I, more than 24 hours ago. This person is incapable of reading documentation or taking advice.. therefore I gave up. I suggest you do too.

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #18

                          J4amieC wrote:

                          therefore I gave up. I suggest you do too.

                          I agree, very good advice. However I do live in the (somewhat vain) hope, that if I keep saying "look at MSDN", "try this", "use Google", "read a tutorial on CP" etc., some of these people may actually come to realize that they could probably solve 99% of their problems by the simple application of a little intelligence and hard work, rather than just posting questions here, then sitting back and waiting for the answer to drop in their lap.

                          J 2 Replies Last reply
                          0
                          • S Sanket Patil

                            Hi J4amieC, Here is my code to review :-

                            private void cmbCompanyName_SelectedIndexChanged(object sender, EventArgs e)
                            {
                            try
                            {
                            cmbFinacialYear.DataSource = null;
                            cmbFinacialYear.DataSource = (from n in logUserDc.mas_COMPANies where n.CM_COMP_ID == Convert.ToInt32(cmbCompanyName.SelectedValue) orderby n.CM_CODE descending select new { Sdate = " From : " + n.CM_ST_DATE + " - To : " + n.CM_END_DATE, Code = n.CM_CODE });
                            cmbFinacialYear.DisplayMember = "Sdate";
                            cmbFinacialYear.ValueMember = "Code";
                            cmbFinacialYear.SelectedIndex = -1;
                            }
                            catch (Exception ex)
                            {
                            objcon.WriteLog("Login Master", "btnLogin_Click", ex);
                            }
                            }

                            Hope you will find the better Solution From these. Thanks for the Help.... Thanks & Regards -- --- ---- Sanket Patil.

                            .

                            J Offline
                            J Offline
                            J4amieC
                            wrote on last edited by
                            #19

                            Change this line:

                            cmbFinacialYear.DataSource = (from n in logUserDc.mas_COMPANies where n.CM_COMP_ID == Convert.ToInt32(cmbCompanyName.SelectedValue) orderby n.CM_CODE descending select new { Sdate = " From : " + n.CM_ST_DATE + " - To : " + n.CM_END_DATE, Code = n.CM_CODE });

                            to

                            cmbFinacialYear.DataSource = (from n in logUserDc.mas_COMPANies where n.CM_COMP_ID == Convert.ToInt32(cmbCompanyName.SelectedValue) orderby n.CM_CODE descending select new { Sdate = String.Format("From: {0:dd/MM/yyyy} - To: {1:dd/MM/yyyy}",n.CM_ST_DATE,n.CM_END_DATE), Code = n.CM_CODE });

                            Im not going to bother explaining why. I just want this question to die already.

                            1 Reply Last reply
                            0
                            • L Lost User

                              J4amieC wrote:

                              therefore I gave up. I suggest you do too.

                              I agree, very good advice. However I do live in the (somewhat vain) hope, that if I keep saying "look at MSDN", "try this", "use Google", "read a tutorial on CP" etc., some of these people may actually come to realize that they could probably solve 99% of their problems by the simple application of a little intelligence and hard work, rather than just posting questions here, then sitting back and waiting for the answer to drop in their lap.

                              J Offline
                              J Offline
                              J4amieC
                              wrote on last edited by
                              #20

                              You're right. So I took pity, and tried once more for a solution.

                              1 Reply Last reply
                              0
                              • L Lost User

                                J4amieC wrote:

                                therefore I gave up. I suggest you do too.

                                I agree, very good advice. However I do live in the (somewhat vain) hope, that if I keep saying "look at MSDN", "try this", "use Google", "read a tutorial on CP" etc., some of these people may actually come to realize that they could probably solve 99% of their problems by the simple application of a little intelligence and hard work, rather than just posting questions here, then sitting back and waiting for the answer to drop in their lap.

                                J Offline
                                J Offline
                                J4amieC
                                wrote on last edited by
                                #21

                                What a surprise, our OP just doesnt bother to respond to 2 serious answers.

                                L 1 Reply Last reply
                                0
                                • J J4amieC

                                  What a surprise, our OP just doesnt bother to respond to 2 serious answers.

                                  L Offline
                                  L Offline
                                  Lost User
                                  wrote on last edited by
                                  #22

                                  J4amieC wrote:

                                  What a surprise, our OP just doesnt bother to respond to 2 serious answers.

                                  Patience brother! ;)

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups