问题描述
请参阅下面的代码
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)) )))
上面代码中的
== Convert.ToDateTime(DateTime.Now.AddDays(-1).ToString(yyyy-MM-dd))) )这是错误的,我可以写出正确的一个
就像2014-12-03。如果我没有转换它的给定错误pld帮助我
我需要用2014-12-03格式写入条件bcz我的数据只有这种格式的数据库
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
推荐答案
foreach (var cstatus in DMStatusReports_D.DM_CI_STATUS.Where(cs => cs.WorkedOn == dateonly ))
{
//do something
}
取决于您可以设置dateonly字段值的数据类型,如下所示
如果您的数据库WorkedOn是varchar则使用
depending on the data type you can set the value of dateonly field as below
if your database WorkedOn is varchar then use
string dateonly = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
如果你的da tabase WorkedOn是Date列然后
if your database WorkedOn is a Date column then
DateTime dateonly = DateTime.Now.Date;
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>
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
}
这篇关于没有获得像“2014-12-03”这样的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!