Linq to Sql今天只能从db获取数据

时间:2015-10-30 12:11:58

标签: c# sql entity-framework linq linq-to-entities

我无法从仅使用日期比较的数据库中获取数据。在Db InDateTime数据类型为DateTime

using (HatronEntities context = new HatronEntities())
{
     DateTime date = DateTime.Now.Date;
     var AttendData = (from c in context.tbl_CoachMobAttendDetails
                       where c.CoachId == model.Id && c.InDateTime.Value.Date ==date
                       select c).FirstOrDefault();
}

4 个答案:

答案 0 :(得分:0)

您可以尝试这样:

`private void Kundenverwaltung_Load(object sender, EventArgs e)
{
    this.dataGridView1.DataSource = this.customerBindingSource;
    this.customerTableAdapter.Fill(this.kundenAnsicht.customer);
}

private void button2_Click(object sender, EventArgs e)
{
    this.customerTableAdapter.Fill(this.kundenAnsicht.customer);
}`

或喜欢

c.InDateTime.Year == date.Year && c.InDateTime.Month == date.Month 
&& c.InDateTime.Day == date.Day

答案 1 :(得分:0)

尝试Like this

c.InDateTime.Value.Date.Year == date.Year &&
c.InDateTime.Value.Date.Month == date.Month &&
c.InDateTime.Value.Date.Day == date.Day

答案 2 :(得分:0)

using (HatronEntities context = new HatronEntities())
{
    DateTime date = DateTime.Today;
    DateTime until = date.AddDays(1);
    var AttendData = (from c in context.tbl_CoachMobAttendDetails
                        where c.CoachId == model.Id && 
                        c.InDateTime.Value >= date &&
                        c.InDateTime.Value < until
                        select c).FirstOrDefault();
}

你最初没有说它是Linq To Entity Framework - 但是你说Linq To SQL!然后:

using (HatronEntities context = new HatronEntities())
{
    DateTime date = DateTime.Today;
    var AttendData = (from c in context.tbl_CoachMobAttendDetails
                        where c.CoachId == model.Id && 
                        DbFunctions.TruncateTime( c.InDateTime ) == date
                        select c).FirstOrDefault();
}

答案 3 :(得分:0)

您可以使用DbFunctions.TruncateTime方法:

DbFunctions.TruncateTime(c.InDateTime.Value.Date) == date.Date