(LINQ to Entities)使用日期判断条件Truncate日期函数

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());




特别说明:
SqlFunctions的命名空间使用:
using System.Data.Entity.SqlServer;
而不是使用:
using System.Data.Objects.SqlClient;

猜你喜欢

转载自blog.csdn.net/afjaklsdflka/article/details/79844554