Converting Long Date To Short Date Format in Linq Query
-
Sanket.Patil wrote:
Please Help me to solve my problem.
I already answered this question. If you want to see the date displayed in a particular format then you need to write the code to do it. The only code you have shown us is the query that is extracting the data from its source. How are you displaying it?
Hi Richard, I am binding this query to datasource of ComboBox and adding DisplayMember and Valuemember to that Combobox. Well I am using C#.NET. The Date is Being Displayed on The ComboBox. The code is As Shown : CODE : 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; Here Sdate contains the result of the query which contains the date format that i dont want. Please Reply Soon. Thanks & Regards -- --- ---- Sanket. Patil
.
-
Hi Richard, I am binding this query to datasource of ComboBox and adding DisplayMember and Valuemember to that Combobox. Well I am using C#.NET. The Date is Being Displayed on The ComboBox. The code is As Shown : CODE : 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; Here Sdate contains the result of the query which contains the date format that i dont want. Please Reply Soon. Thanks & Regards -- --- ---- Sanket. Patil
.
Sanket.Patil wrote:
Here Sdate contains the result of the query which contains the date format that i dont want.
Take a look at FormatString property on the ComboBox, it offers various default formats for DateTime, including the one that you are interested in.
-
Sanket.Patil wrote:
Here Sdate contains the result of the query which contains the date format that i dont want.
Take a look at FormatString property on the ComboBox, it offers various default formats for DateTime, including the one that you are interested in.
Hi Richard, Thanks For Reply. I Tried on the solution you gave but the problem is the text of the ComboBox shows me the value as shown below:- " From : Apr 1 2010 12:00AM - To : Mar 31 2011 12:00AM ". Which I dont want. It Should Show me the text as follow:- " From : 1/Apr/2010 - To : 31/Mar/2011 ". So is it possible to convert the query result to the result that i want before providing displaymember to ComboBox. Please Reply.... Thanks & Regards -- --- ---- Sanket Patil
.
-
Hi Richard, Thanks For Reply. I Tried on the solution you gave but the problem is the text of the ComboBox shows me the value as shown below:- " From : Apr 1 2010 12:00AM - To : Mar 31 2011 12:00AM ". Which I dont want. It Should Show me the text as follow:- " From : 1/Apr/2010 - To : 31/Mar/2011 ". So is it possible to convert the query result to the result that i want before providing displaymember to ComboBox. Please Reply.... Thanks & Regards -- --- ---- Sanket Patil
.
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
andn.CM_END_DATE
. UsingString.Format()
may be the better option than just concatenating from theToString()
methods. -
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.
.
-
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
.
-
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
andn.CM_END_DATE
. UsingString.Format()
may be the better option than just concatenating from theToString()
methods.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
.
-
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
.
-
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
.
-
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.
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
.
-
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.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.
.
-
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
.
You have posted the original code twice and in both cases you are creating
Sdate
as a concatenation of various fields includingn.CM_ST_DATE
. However this latter field will be returning its defaultToString()
method rather than the formatted version that you require. Tryn.CM_ST_DATE.ToString("D")
, or some alternateDateTime
format string to get the date only. [EDIT]above should readn.CM_ST_DATE.ToString("d")
.[/EDIT]modified on Tuesday, October 6, 2009 8:41 AM
-
You have posted the original code twice and in both cases you are creating
Sdate
as a concatenation of various fields includingn.CM_ST_DATE
. However this latter field will be returning its defaultToString()
method rather than the formatted version that you require. Tryn.CM_ST_DATE.ToString("D")
, or some alternateDateTime
format string to get the date only. [EDIT]above should readn.CM_ST_DATE.ToString("d")
.[/EDIT]modified on Tuesday, October 6, 2009 8:41 AM
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
.
-
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
.
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)
(whereformatstring
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. -
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)
(whereformatstring
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. -
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.
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.
-
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.
.
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.
-
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.
-
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.