使用实体框架比较日期部分日期

时间:2018-05-02 05:25:30

标签: asp.net entity-framework

我想使用实体框架获取当月的数据,数据库有一个名为Date的字段,但我需要根据特定人的当前月份获取数据。

[WebMethod]
public string getAttendanceofUserofMonthbyId(int id)
{
    string Date;
    List<AttendenceGetSet> lst = new List<AttendenceGetSet>();
    List<AttendenceGetSet> lst2 = new List<AttendenceGetSet>();

    AttendenceGetSet gs = new AttendenceGetSet();

    //Date = DateTime.Now.ToString("dd/MM/yyyy").Replace("-", "/");
    string month = DateTime.Now.Month.ToString();
    usersGetSet usergetset = new usersGetSet();

    using (SoubhagyatechEntities dbcontext = new SoubhagyatechEntities())
    {

        lst = dbcontext.tblempAttendences.Where(i => i.Date == month && i.userId == id).Select(i => new AttendenceGetSet
        {

            userAddress = i.userAddress,
            userLatitude = i.userLatitude,
            userLongitude = i.userLongitude,
            Date = i.Date,
            userId = i.userId,
            Time = i.Time,
            empAttendenceId = i.empAttendenceId,

            username = dbcontext.tblusers.Where(j => j.userId == id && j.userStatus == true).Select(j => j.userFullName).FirstOrDefault()



        }).ToList();

    }

    JavaScriptSerializer jss = new JavaScriptSerializer();
    string jsons = jss.Serialize(lst);
    return jsons;
}

1 个答案:

答案 0 :(得分:0)

请您尝试下面的代码吗?

var mindate = new DateTime(now.Year, now.Month, 1);
var maxdate = first.AddMonths(1).AddDays(-1);
dbcontext.tblempAttendences.Where(i => i.Date >=mindate && i.Date<=maxdate && i.userId == id)