linq to entities查询使用大于小于匹配数据库字符串日期方法
Entity Framework(LINQ to Entities)使用日期判断条件Truncate日期函数,类似Convert函数,MYSQL数据库可以直接使用convert(varchar(10),a.cjrq,120)
LINQ to Entities使用日期字段查询,截取日期,不包括时间,测试环境SQL Server2005/2008,Entity Framework4.0
public void TestMethod1()
{
using (var _context = new hotelEntities())
{
var rq = DateTime.Now.Date;
var query = from q in _context.UV_RZJL_RZRY_Single
where EntityFunctions.TruncateTime(q.LDRQ) >=rq
select q;
Assert.Inconclusive(query.Count().ToString());
}
}
//SqlFunctions.DateDiff的函数也可以实现
public void TestMethod1()
{
using (var _context = new hotelEntities())
{
var rq = DateTime.Now.Date;
var query = from q in _context.UV_RZJL_RZRY_Single
where SqlFunctions.DateDiff("day",rq,q.LDRQ)>0
select q;
Assert.Inconclusive(query.Count().ToString());
}
}筛选本周数据
//获取当天的数据
DrawRecordDA _recordDA = new DrawRecordDA();
var query = _recordDA.GetQuery();
//筛选 当天
//query = query.Where(q=>SqlFunctions.DateDiff("day",q.AddTime,DateTime.Now)==0);
//筛选 当天
// query = query.Where(q=>q.AddTime.Day==DateTime.Now.Day);
//筛选 本周
query = query.Where(q => SqlFunctions.DateDiff("week", q.AddTime, DateTime.Now) == 0);
Console.WriteLine(query.Count());
var list=(from j in db.实体
where (int)j.date as >20140503&&(int)j.date<20140510).ToList();
date的长度如果大于10要强转成long
嗯,忘了跟linq数据不能C#方法处理。那就用Contains()