dotnet-dynamic-linq/README.md

200 lines
5.9 KiB
Markdown
Raw Normal View History

2018-02-11 20:52:44 -05:00
# DynamicLinq
Adds extensions to Linq to offer dynamic queryables.
2018-02-11 21:42:05 -05:00
2018-02-13 12:31:06 -05:00
## Roadmap
Check "Projects" section of github to see whats going on.
2018-02-13 12:32:23 -05:00
2018-02-13 12:32:02 -05:00
https://github.com/PoweredSoft/DynamicLinq/projects/1
2018-02-11 21:42:05 -05:00
## Download
Full Version | NuGet | NuGet Install
------------ | :-------------: | :-------------:
2018-03-01 09:48:09 -05:00
PoweredSoft.DynamicLinq | <a href="https://www.nuget.org/packages/PoweredSoft.DynamicLinq/" target="_blank">[![NuGet](https://img.shields.io/nuget/v/PoweredSoft.DynamicLinq.svg?style=flat-square&label=nuget)](https://www.nuget.org/packages/PoweredSoft.DynamicLinq/)</a> | ```PM> Install-Package PoweredSoft.DynamicLinq```
PoweredSoft.DynamicLinq.EntityFramework | <a href="https://www.nuget.org/packages/PoweredSoft.EntityFramework/" target="_blank">[![NuGet](https://img.shields.io/nuget/v/PoweredSoft.DynamicLinq.EntityFramework.svg?style=flat-square&label=nuget)](https://www.nuget.org/packages/PoweredSoft.DynamicLinq.EntityFramework/)</a> | ```PM> Install-Package PoweredSoft.DynamicLinq.EntityFramework```
2018-02-12 22:31:49 -05:00
## Samples
2018-02-11 21:42:05 -05:00
Complex Query
2018-02-11 21:44:19 -05:00
```csharp
2018-02-11 21:42:05 -05:00
query = query.Query(q =>
{
q.Compare("AuthorId", ConditionOperators.Equal, 1);
q.And(sq =>
{
sq.Compare("Content", ConditionOperators.Equal, "World");
sq.Or("Title", ConditionOperators.Contains, 3);
});
});
2018-02-11 21:42:55 -05:00
```
2018-02-28 23:30:02 -05:00
### Shortcuts
Shortcuts allow to avoid specifying the condition operator by having it handy in the method name
```csharp
queryable.Query(t => t.Contains("FirstName", "Dav").OrContains("FirstName", "Jo"));
```
You may visit this test for more examples: https://github.com/PoweredSoft/DynamicLinq/blob/master/PoweredSoft.DynamicLinq.Test/ShortcutTests.cs
2018-02-12 22:33:01 -05:00
### Simple Query
2018-02-11 21:44:19 -05:00
```csharp
2018-02-11 21:42:55 -05:00
query.Where("FirstName", ConditionOperators.Equal, "David");
2018-02-11 21:44:19 -05:00
```
2018-02-12 04:18:42 -05:00
2018-03-14 20:51:31 -04:00
### Grouping Support
```csharp
TestData.Sales
.AsQueryable()
.GroupBy(t => t.Path("ClientId"))
.Select(t =>
{
t.Key("TheClientId", "ClientId");
t.Count("Count");
t.LongCount("LongCount");
t.Sum("NetSales");
t.Average("Tax", "TaxAverage");
t.ToList("Sales");
});
2018-03-14 20:53:58 -04:00
```
Is equivalent to
```csharp
2018-03-14 20:53:33 -04:00
TestSales
.GroupBy(t => new { t.ClientId })
.Select(t => new {
TheClientId = t.Key.ClientId,
Count = t.Count(),
LongCount = t.LongCount(),
NetSales = t.Sum(t2 => t2.NetSales),
TaxAverage = t.Average(t2 => t2.Tax),
Sales = t.ToList()
});
2018-04-15 00:41:22 -04:00
```
### Select
```csharp
var querySelect = query.Select(t =>
{
t.NullChecking(true); // not obligated but usefull for in memory queries.
t.PathToList("Posts.Comments.CommentLikes", selectCollectionHandling: SelectCollectionHandling.Flatten);
t.Path("FirstName");
t.Path("LastName", "ChangePropertyNameOfLastName");
});
```
2018-03-14 20:51:31 -04:00
2018-03-06 22:09:24 -05:00
### In Support
You can filter with a list, this will generate a contains with your list.
```csharp
var ageGroup = new List<int>() { 28, 27, 50 };
Persons.AsQueryable().Query(t => t.In("Age", ageGroup));
```
### String Comparision Support
```csharp
Persons.AsQueryable().Query(t => t.Equal("FirstName", "DAVID", stringComparision: StringComparison.OrdinalIgnoreCase));
```
You may visit this test for more examples:
https://github.com/PoweredSoft/DynamicLinq/blob/master/PoweredSoft.DynamicLinq.Test/StringComparision.cs
2018-02-12 22:33:01 -05:00
### Simple Sorting
2018-02-12 05:20:15 -05:00
```csharp
query = query.OrderByDescending("AuthorId");
query = query.ThenBy("Id");
```
2018-02-12 22:33:01 -05:00
### Collection Filtering
2018-02-12 22:31:49 -05:00
You don't have to Worry about it.
2018-02-12 23:23:43 -05:00
The library will do it for you.
2018-02-12 22:28:35 -05:00
```csharp
var query = authors.AsQueryable();
query = query.Query(qb =>
{
qb.NullChecking();
2018-02-12 22:31:49 -05:00
// you can specify here which collection handling you wish to use Any and All is supported for now.
2018-02-12 22:28:35 -05:00
qb.And("Posts.Comments.Email", ConditionOperators.Equal, "john.doe@me.com", collectionHandling: QueryCollectionHandling.Any);
});
```
2018-02-12 22:34:09 -05:00
### Null Checking is automatic (practical for in memory dynamic queries)
2018-02-12 22:31:49 -05:00
```csharp
var query = authors.AsQueryable();
query = query.Query(qb =>
{
qb.NullChecking();
qb.And("Posts.Comments.Email", ConditionOperators.Equal, "john.doe@me.com", collectionHandling: QueryCollectionHandling.Any);
});
```
2018-02-12 22:33:01 -05:00
### Using Query Builder
2018-02-12 04:18:42 -05:00
```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();
2018-02-12 23:23:43 -05:00
```
2018-02-14 00:13:01 -05:00
### Entity Framework
Using PoweredSoft.DynamicLinq.EntityFramework it adds an helper that allows you to do the following.
```csharp
var context = new <YOUR CONTEXT>();
var queryable = context.Query(typeof(Author), q => q.Compare("FirstName", ConditionOperators.Equal, "David"));
var result = queryable.ToListAsync().Result;
var first = result.FirstOrDefault() as Author;
Assert.AreEqual(first?.FirstName, "David");
```
2018-03-07 20:59:37 -05:00
### How it can be used in a web api
```csharp
[HttpGet][Route("FindClients")]
2018-03-07 21:00:00 -05:00
public IHttpActionResult FindClients(string filterField = null, string filterValue = null,
string sortProperty = "Id", int? page = null, int pageSize = 50)
{
var ctx = new MyDbContext();
var query = ctx.Clients.AsQueryable();
if (!string.IsNullOrEmpty(filterField) && !string.IsNullOrEmpty(filterValue))
query = query.Query(t => t.Contains(filterField, filterValue)).OrderBy(sortProperty);
// count.
var clientCount = query.Count();
int? pages = null;
if (page.HasValue && pageSize > 0)
{
if (clientCount == 0)
pages = 0;
else
pages = clientCount / pageSize + (clientCount % pageSize != 0 ? 1 : 0);
}
if (page.HasValue)
query = query.Skip((page.Value-1) * pageSize).Take(pageSize);
var clients = query.ToList();
return Ok(new
{
total = clientCount,
pages = pages,
data = clients
});
}
2018-03-07 20:59:37 -05:00
```