Click here to Skip to main content
15,904,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
pls see the below code

string dateonly = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
foreach (var cstatus in DMStatusReports_D.DM_CI_STATUS.Where(cs => cs.WorkedOn == Convert.ToDateTime(DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd"))))

in above code == Convert.ToDateTime(DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd")))) this is giving error ia m nt able to write correct one

ineed like 2014-12-03. if I don't convert its giving error pld help me

I need to write condition with 2014-12-03 format bcz my data in database in this format only
Posted
Comments
Richard MacCutchan 4-Dec-14 4:46am    
What error?
Praveen Kumar Upadhyay 4-Dec-14 4:48am    
What is DMStatusReports_D.DM_CI_STATUS

What types of values it has.
jayanthik 4-Dec-14 5:20am    
DMStatusReports_D.DM_CI_STATUS is entity data model. I have WorkedOn is a column in my table with type is 'date' I need to get data based on this date
WorkedOn is format is like this 2014-12-03
jayanthik 4-Dec-14 5:20am    
getting error Cannot implicitly convert type 'string' to 'System.DateTime?'

use dateonly value as below
C#
foreach (var cstatus in DMStatusReports_D.DM_CI_STATUS.Where(cs => cs.WorkedOn == dateonly ))
{
   //do something 
}


depending on the data type you can set the value of dateonly field as below
if your database WorkedOn is varchar then use
C#
string dateonly = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");

if your database WorkedOn is a Date column then
C#
DateTime  dateonly = DateTime.Now.Date;
 
Share this answer
 
v2
Comments
jayanthik 4-Dec-14 5:17am    
my database workedon is date type
DamithSL 4-Dec-14 5:19am    
then set the dateonly as DateTime.Now.Date and use that value in your linq statement
jayanthik 4-Dec-14 5:30am    
great thank you so much
BillWoodruff 4-Dec-14 10:41am    
If this solution answered your question be sure and accept it, and vote on it. That's a way you can contribute to quality on CodeProject.
BillWoodruff 4-Dec-14 10:40am    
+5
This code will help you to solve your problem. Happy coding!

private void button1_Click(object sender, EventArgs e)
       {

           List<emp> lstEmp = new List<emp>();
           Emp e4 = new Emp();

           e4.Name = "Anis";
           e4.StartDate =DateTime.Parse("2014-12-03");

           lstEmp.Add(e4);

           Emp e1 = new Emp();

           e1.Name = "rakib";
           e1.StartDate = DateTime.Parse("2014-12-23");

           lstEmp.Add(e1);

           Emp e2 = new Emp();

           e2.Name = "Toma";
           e2.StartDate = DateTime.Parse("2014-10-01");

           lstEmp.Add(e2);

           Emp e3 = new Emp();

           e3.Name = "Sumon";
           e3.StartDate = DateTime.Parse("2014-12-03");

           lstEmp.Add(e3);

           string dt = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
           DateTime t = new DateTime();
           t = Convert.ToDateTime(dt);

           foreach (Emp em in lstEmp.Where(x=>x.StartDate==t))
           {
               MessageBox.Show(em.Name.ToString());

           }




       }

       public class Emp
       {
           public string Name { get; set; }
           public DateTime StartDate { get; set; }

       }</emp></emp>
 
Share this answer
 
v2
Try this:

C#
string dateonly = DateTime.Now.ToString();
string createddate = Convert.ToDateTime(dateonly).ToString("yyyy-MM-dd"); 

foreach (var cstatus in DMStatusReports_D.DM_CI_STATUS.Where(cs => cs.WorkedOn == createddate))
{
    //do code
}
 
Share this answer
 
Comments
jayanthik 4-Dec-14 5:16am    
I tried above code getting error Error 1
Cannot implicitly convert type 'string' to 'System.DateTime?'

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900