public void Test_PostRepository_FindAllOrderByWithoutPaging() { PostQueryRequest request = new PostQueryRequest(); request.TopicId = 1000; request.CreationDateTimeParam.CreationDateTimeOperator = Operator.LessThanEqual; request.CreationDateTimeParam.CreationDateTime = DateTimeUtils.ToDateTime("2015-1-22").Value; using (IRepositoryContext repositoryContext = ServiceLocator.Instance.GetService<IRepositoryContext>()) { IRepository<Post> postRepository = repositoryContext.GetRepository<Post>(); Expression<Func<Post, bool>> dateTimeExpression = (p) => true; //DateTime dt = request.CreationDateTimeParam.CreationDateTime; switch (request.CreationDateTimeParam.CreationDateTimeOperator) { case Operator.LessThanEqual: dateTimeExpression = p => p.CreationDateTime <= request.CreationDateTimeParam.CreationDateTime; break; case Operator.GreaterThanEqual: dateTimeExpression = p => p.CreationDateTime >= request.CreationDateTimeParam.CreationDateTime; break; case Operator.Equal: dateTimeExpression = p => p.CreationDateTime.Equals(request.CreationDateTimeParam.CreationDateTime); break; } QueryBuilder<Post> postQueryBuilder = new QueryBuilder<Post>(); //int topicId = request.TopicId; postQueryBuilder.And(p => p.TopicId == request.TopicId).And(dateTimeExpression); IEnumerable<Post> posts = postRepository.FindAll(postQueryBuilder.QueryPredicate); IList<PostDataObject> postDataObjects = new List<PostDataObject>(); foreach (Post post in posts) { var postDataObject = new PostDataObject(); postDataObject.MapFrom(post); postDataObjects.Add(postDataObject); } Assert.AreEqual(5, postDataObjects.Count); } }
public IEnumerable<PostDataObject> GetPostsByQueryRequest(PostQueryRequest request) { using (IRepositoryContext repositoryContext = ServiceLocator.Instance.GetService<IRepositoryContext>()) { IRepository<Post> postRepository = repositoryContext.GetRepository<Post>(); Expression<Func<Post, bool>> dateTimeExpression = (p) => true; switch (request.CreationDateTimeParam.CreationDateTimeOperator) { case Operator.LessThanEqual: dateTimeExpression = p => p.CreationDateTime <= request.CreationDateTimeParam.CreationDateTime; break; case Operator.GreaterThanEqual: dateTimeExpression = p => p.CreationDateTime >= request.CreationDateTimeParam.CreationDateTime; break; case Operator.Equal: default: dateTimeExpression = p => p.CreationDateTime.Equals(request.CreationDateTimeParam.CreationDateTime); break; } QueryBuilder<Post> postQueryBuilder = new QueryBuilder<Post>(); postQueryBuilder.And(p => p.TopicId == request.TopicId).And(dateTimeExpression); IEnumerable<Post> posts = postRepository.FindAll(postQueryBuilder.QueryPredicate); IList<PostDataObject> postDataObjects = new List<PostDataObject>(); foreach (Post post in posts) { var postDataObject = new PostDataObject(); postDataObject.MapFrom(post); postDataObjects.Add(postDataObject); } return postDataObjects; } }
public void Test_LambdaBuilderWhereClause_CombineCondition_QueryValueWithPropertyWithCallMethod() { Database db = new Database(Eagle.Data.DbProviderFactory.Default); WhereClauseBuilder<Post> builder = db.DBProvider.CreateWhereClauseBuilder<Post>(); PostQueryRequest request = new PostQueryRequest(); request.TopicId = 1000; request.MiscParams.TopicId = 2000; request.Content = "足球"; request.MiscParams.Content = "篮球"; request.CreationDateTimeParam.CreationDateTime = DateTimeUtils.ToDateTime("2015-1-22").Value; WhereClauseBuildResult result = builder.BuildWhereClause(p => (p.TopicId < request.TopicId || p.TopicId > request.MiscParams.TopicId) && (p.Content.Contains(request.Content) || p.Content.Contains(request.MiscParams.Content)) || (p.CreationDateTime < request.CreationDateTimeParam.CreationDateTime || p.CreationDateTime.Equals(request.CreationDateTimeParam.CreationDateTime))); string orderBy = builder.BuildOrderByClause(p => p.CreationDateTime); Assert.AreEqual(true, !string.IsNullOrEmpty(result.WhereClause)); Assert.AreEqual(1000, result.ParameterValues.Values.ToList()[0]); Assert.AreEqual(2000, result.ParameterValues.Values.ToList()[1]); Assert.AreEqual("%足球%", result.ParameterValues.Values.ToList()[2]); Assert.AreEqual("%篮球%", result.ParameterValues.Values.ToList()[3]); Assert.AreEqual(request.CreationDateTimeParam.CreationDateTime, result.ParameterValues.Values.ToList()[4]); Assert.AreEqual(request.CreationDateTimeParam.CreationDateTime, result.ParameterValues.Values.ToList()[5]); }
public void Test_LambdaBuilderWhereClause_QueryValueWithPropertyWithCallMethod() { Database db = new Database(Eagle.Data.DbProviderFactory.Default); WhereClauseBuilder<Post> builder = db.DBProvider.CreateWhereClauseBuilder<Post>(); PostQueryRequest request = new PostQueryRequest(); request.TopicId = 1000; request.Content = "足球"; request.CreationDateTimeParam.CreationDateTime = DateTimeUtils.ToDateTime("2015-1-22").Value; List<int> topicIds = new List<int>(); topicIds.Add(1000); topicIds.Add(2000); topicIds.Add(3000); WhereClauseBuildResult result = builder.BuildWhereClause(p => p.TopicId.Equals(request.TopicId) && p.Content.Contains(request.Content) && p.CreationDateTime < request.CreationDateTimeParam.CreationDateTime); string orderBy = builder.BuildOrderByClause(p => p.CreationDateTime); Assert.AreEqual(true, !string.IsNullOrEmpty(result.WhereClause)); Assert.AreEqual(1000, result.ParameterValues.Values.ToList()[0]); Assert.AreEqual("%足球%", result.ParameterValues.Values.ToList()[1]); Assert.AreEqual(request.CreationDateTimeParam.CreationDateTime, result.ParameterValues.Values.ToList()[2]); }