Asp.net 使用实体框架比较日期的月份部分

Asp.net 使用实体框架比较日期的月份部分,asp.net,entity-framework,Asp.net,Entity Framework,我想使用实体框架获取当月数据,数据库中有一个名为Date的字段,但我需要基于特定人员的当月获取数据 [WebMethod] public string getAttendanceofUserofMonthbyId(int id) { string Date; List<AttendenceGetSet> lst = new List<AttendenceGetSet>(); List<AttendenceGetSet> lst2 = n

我想使用实体框架获取当月数据,数据库中有一个名为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;
}
[WebMethod]
公共字符串GetAttendanceFuseRofMonthById(int id)
{
字符串日期;
List lst=新列表();
List lst2=新列表();
AttendanceGetSet gs=新的AttendanceGetSet();
//Date=DateTime.Now.ToString(“dd/MM/yyyy”)。替换(“-”,“/”);
字符串month=DateTime.Now.month.ToString();
usersGetSet usergetset=new usersGetSet();
使用(soubhagyatechenties dbcontext=new soubhagyatechenties())
{
lst=dbcontext.tblempAttendences.Where(i=>i.Date==month&&i.userId==id)。选择(i=>newattendencegetset
{
userAddress=i.userAddress,
userLatitude=i.userLatitude,
userLongitude=i.userLongitude,
日期=i.日期,
userId=i.userId,
时间,
empattenceid=i.empattenceid,
username=dbcontext.tblusers.Where(j=>j.userId==id&&j.userStatus==true)。选择(j=>j.userFullName)。FirstOrDefault()
}).ToList();
}
JavaScriptSerializer jss=新的JavaScriptSerializer();
字符串jsons=jss.Serialize(lst);
返回JSON;
}

请尝试下面的代码好吗

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)
var mindate=new DateTime(now.Year,now.Month,1);
var maxdate=first.AddMonths(1).AddDays(-1);

dbcontext.tblempAttendences.Where(i=>i.Date>=mindate和&i.Datelist返回emptyCan请尝试此行
dbcontext.tblempAttendences.Where(i=>i.Date.Month==DateTime.Now.Month和&i.userId==id)
?我假设您的日期在数据库中存储为DateTime。错误:'string'不包含'Month'的定义,并且找不到接受'string'类型的第一个参数的扩展方法'Month'(是否缺少using指令或程序集引用?)有很多事情可能会出错。您不应该在数据库中以字符串形式存储日期,理想情况下应该是datetime。您是否可以编写SQL查询并检查数据库中是否确实有符合where条件的数据。数据库中的数据格式为“dd/MM/YYYY”,字段名为“Date”