linq to entities查询使用大于小于匹配数据库字符串日期方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linq to entities查询使用大于小于匹配数据库字符串日期方法相关的知识,希望对你有一定的参考价值。

这写日期的格式类是在数据库里是char类型,然后我想使用linq to entities查询20140503至20140510这之间的数据,由于格式问题,请教高手解答怎么去匹配条件呢?我返回的是IEnumerable

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

参考技术A private EntitiesContext db = new EntitiesContext();
var list=(from j in db.实体
where (int)j.date as >20140503&&(int)j.date<20140510).ToList();
date的长度如果大于10要强转成long追问

Convert.ToInt32(q.date)我准备用这个,执行的提示LINQ to Entities 无法辨识方法 'Int32 ToInt32(System.String)' 方法,而且这个方法无法转译成存放区运算式。

追答

嗯,忘了跟linq数据不能C#方法处理。那就用Contains()

追问

这不是模糊查询吗?

追答

string不能转换成int的话就只能用匹配的方法了,linq要转换成IL的,所以不能用C#的方法处理数据库的数据。

追问

也只能这样处理了,因为我数据存储的数据非常庞大,从前年就开始有数据了,所以我只能是模糊匹配大概月份,查询出List数据集了,再针对这个List去做筛选需要的!虽然的可以实现,但是也大大降低了效率!

追答

这样,先用date=20140503和date=20140510的项的id,然后在用id查询这两个id之前的数据

追问

最坑爹的就是,这张表没有ID!算了,朋友谢谢你了。我对这个linq查询也是刚接触,感觉用得很吃力!但是还是谢啦。

本回答被提问者采纳

Linq To Entity 查询条件扩展

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Linq.Expressions;
  5 using System.Web;
  6 
  7 namespace Test
  8 {
  9     /// <summary>
 10     /// 统一ParameterExpression
 11     /// </summary>
 12     public class ParameterReplacer : ExpressionVisitor
 13     {
 14         public ParameterReplacer(ParameterExpression paramExpr)
 15         {
 16             ParameterExpression = paramExpr;
 17         }
 18 
 19         public ParameterExpression ParameterExpression { get; private set; }
 20 
 21         public Expression Replace(Expression expr)
 22         {
 23             return Visit(expr);
 24         }
 25 
 26         protected override Expression VisitParameter(ParameterExpression p)
 27         {
 28             return ParameterExpression;
 29         }
 30     }
 31     /// <summary>
 32     /// Predicate扩展
 33     /// </summary>
 34     public static class PredicateExtensionses
 35     {
 36         /// <summary>
 37         /// 
 38         /// </summary>
 39         /// <typeparam name="T"></typeparam>
 40         /// <returns></returns>
 41         public static Expression<Func<T, bool>> True<T>() { return f => true; }
 42         /// <summary>
 43         /// 
 44         /// </summary>
 45         /// <typeparam name="T"></typeparam>
 46         /// <returns></returns>
 47         public static Expression<Func<T, bool>> False<T>() { return f => false; }
 48         /// <summary>
 49         /// 
 50         /// </summary>
 51         /// <typeparam name="T"></typeparam>
 52         /// <param name="expLeft"></param>
 53         /// <param name="expRight"></param>
 54         /// <returns></returns>
 55         public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expLeft, Expression<Func<T, bool>> expRight)
 56         {
 57             var candidateExpr = Expression.Parameter(typeof(T), "candidate");
 58             var parameterReplacer = new ParameterReplacer(candidateExpr);
 59 
 60             var left = parameterReplacer.Replace(expLeft.Body);
 61             var right = parameterReplacer.Replace(expRight.Body);
 62             var body = Expression.And(left, right);
 63 
 64             return Expression.Lambda<Func<T, bool>>(body, candidateExpr);
 65         }
 66         /// <summary>
 67         /// 
 68         /// </summary>
 69         /// <typeparam name="T"></typeparam>
 70         /// <param name="expLeft"></param>
 71         /// <param name="expRight"></param>
 72         /// <returns></returns>
 73         public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expLeft, Expression<Func<T, bool>> expRight)
 74         {
 75             var candidateExpr = Expression.Parameter(typeof(T), "candidate");
 76             var parameterReplacer = new ParameterReplacer(candidateExpr);
 77 
 78             var left = parameterReplacer.Replace(expLeft.Body);
 79             var right = parameterReplacer.Replace(expRight.Body);
 80             var body = Expression.Or(left, right);
 81 
 82             return Expression.Lambda<Func<T, bool>>(body, candidateExpr);
 83         }
 84     }
 85     /// <summary>
 86     /// Queryable扩展
 87     /// </summary>
 88     public static class QueryableExtensions
 89     {
 90         /// <summary>
 91         /// 
 92         /// </summary>
 93         /// <typeparam name="T"></typeparam>
 94         /// <param name="queryable"></param>
 95         /// <param name="propertyName"></param>
 96         /// <returns></returns>
 97         public static IQueryable<T> OrderBy<T>(this IQueryable<T> queryable, string propertyName)
 98         {
 99             return OrderBy(queryable, propertyName, false);
100         }
101         /// <summary>
102         /// OrderBy
103         /// </summary>
104         /// <typeparam name="T">实体</typeparam>
105         /// <param name="queryable">条件</param>
106         /// <param name="propertyName">属性名称</param>
107         /// <param name="desc">是否降序</param>
108         /// <returns></returns>
109         public static IQueryable<T> OrderBy<T>(this IQueryable<T> queryable, string propertyName, bool desc)
110         {
111             var param = Expression.Parameter(typeof(T));
112             var body = Expression.Property(param, propertyName);
113             dynamic keySelector = Expression.Lambda(body, param);
114             return desc ? Queryable.OrderByDescending(queryable, keySelector) : Queryable.OrderBy(queryable, keySelector);
115         }
116     }
117 }

 

以上是关于linq to entities查询使用大于小于匹配数据库字符串日期方法的主要内容,如果未能解决你的问题,请参考以下文章