example using query builder.

This commit is contained in:
David 2018-02-12 03:18:42 -06:00
parent abf15e30c4
commit 8e88e13861
2 changed files with 53 additions and 2 deletions

View File

@ -1,6 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using PoweredSoft.DynamicLinq.Dal.Pocos; using PoweredSoft.DynamicLinq.Dal.Pocos;
using PoweredSoft.DynamicLinq.Extensions; using PoweredSoft.DynamicLinq.Extensions;
using PoweredSoft.DynamicLinq.Fluent;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -16,7 +17,7 @@ namespace PoweredSoft.DynamicLinq.Test
public void ComplexQueryBuilder() public void ComplexQueryBuilder()
{ {
// subject. // subject.
var authors = new List<Post>() var posts = new List<Post>()
{ {
new Post { Id = 1, AuthorId = 1, Title = "Hello 1", Content = "World" }, new Post { Id = 1, AuthorId = 1, Title = "Hello 1", Content = "World" },
new Post { Id = 2, AuthorId = 1, Title = "Hello 2", Content = "World" }, new Post { Id = 2, AuthorId = 1, Title = "Hello 2", Content = "World" },
@ -24,7 +25,7 @@ namespace PoweredSoft.DynamicLinq.Test
}; };
// the query. // the query.
var query = authors.AsQueryable(); var query = posts.AsQueryable();
query = query.Query(q => query = query.Query(q =>
{ {
@ -38,5 +39,31 @@ namespace PoweredSoft.DynamicLinq.Test
Assert.AreEqual(2, query.Count()); Assert.AreEqual(2, query.Count());
} }
[TestMethod]
public void UsingQueryBuilder()
{
// subject.
var posts = new List<Post>()
{
new Post { Id = 1, AuthorId = 1, Title = "Hello 1", Content = "World" },
new Post { Id = 2, AuthorId = 1, Title = "Hello 2", Content = "World" },
new Post { Id = 3, AuthorId = 2, Title = "Hello 3", Content = "World" },
};
// the query.
var query = posts.AsQueryable();
var queryBuilder = new QueryBuilder<Post>(query);
queryBuilder.Compare("AuthorId", ConditionOperators.Equal, 1);
queryBuilder.And(subQuery =>
{
subQuery.Compare("Content", ConditionOperators.Equal, "World");
subQuery.Or("Title", ConditionOperators.Contains, 3);
});
query = queryBuilder.Build();
Assert.AreEqual(2, query.Count());
}
} }
} }

View File

@ -20,3 +20,27 @@ Simple Query
```csharp ```csharp
query.Where("FirstName", ConditionOperators.Equal, "David"); query.Where("FirstName", ConditionOperators.Equal, "David");
``` ```
Using Query Builder
```csharp
// subject.
var posts = new List<Post>()
{
new Post { Id = 1, AuthorId = 1, Title = "Hello 1", Content = "World" },
new Post { Id = 2, AuthorId = 1, Title = "Hello 2", Content = "World" },
new Post { Id = 3, AuthorId = 2, Title = "Hello 3", Content = "World" },
};
// the query.
var query = posts.AsQueryable();
var queryBuilder = new QueryBuilder<Post>(query);
queryBuilder.Compare("AuthorId", ConditionOperators.Equal, 1);
queryBuilder.And(subQuery =>
{
subQuery.Compare("Content", ConditionOperators.Equal, "World");
subQuery.Or("Title", ConditionOperators.Contains, 3);
});
query = queryBuilder.Build();
```