Skip to content

Repository files navigation

KendoNET.DynamicLinq

Version Downloads .NET Standard

Description

KendoNET.DynamicLinq implements server paging, filtering, sorting, grouping, and aggregating to Kendo UI via Dynamic Linq for .NET Core App(1.x ~ 3.x).

Prerequisites

.NET Core 1 ~ 2

  • None

.NET Core 3

  • You must add custom ObjectToInferredTypesConverter to your JsonSerializerOptions since System.Text.Json didn't deserialize inferred type to object properties now, see the sample code and reference.

Usage

  1. Add the KendoNET.DynamicLinq NuGet package to your project.
  2. Configure your Kendo DataSource to send its options as JSON.
parameterMap: function(options, type) {
    return kendo.stringify(options);
}
  1. Configure the schema of the dataSource.
schema: {
    data: "Data",
    total: "Total",
    aggregates: "Aggregates",
    groups: "Groups",
    errors: "Errors"
}
  1. The completed code like below.
..... Other kendo grid code .....

dataSource: {
    schema:
    {
        data: "Data",
        total: "Total",
        aggregates: "Aggregates",
        groups: "Groups",
        errors: "Errors",
        ...
    },
    transport: {
        read: {
            url: 'read url',
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            type: 'POST'
        },
        create: {
            url: 'create url',
            dataType: "json",
            contentType: 'application/json; charset=utf-8',
            type: 'POST'
        },
        parameterMap: function (data, operation) {
            return kendo.stringify(data);
        }
    },
    error: function(e) {
        console.log(e.errors); // Your error information
        e.sender.cancelChanges();
    },
    pageSize: 20,
    serverPaging: true,
    serverFiltering: true,
    serverSorting: true,
    ...
}

..... Other kendo grid code .....
  1. Import the KendoNET.DynamicLinq namespace.
  2. Use the ToDataSourceResult extension method to apply paging, sorting, filtering, grouping and aggregating.
using KendoNET.DynamicLinq

[WebMethod]
public static DataSourceResult Products(int take, int skip, IEnumerable<Sort> sort, Filter filter, IEnumerable<Aggregator> aggregates, IEnumerable<Group> groups)
{
    using (var northwind = new Northwind())
    {
        return northwind.Products
               .OrderBy(p => p.ProductID) // EF requires ordering for paging
               .Select(p => new ProductViewModel // Use a view model to avoid serializing internal Entity Framework properties as JSON
               {
                   ProductID = p.ProductID,
                   ProductName = p.ProductName,
                   UnitPrice = p.UnitPrice,
                   UnitsInStock = p.UnitsInStock,
                   Discontinued = p.Discontinued
               })
               .ToDataSourceResult(take, skip, sort, filter, aggregates, groups);
    }
}

or from Kendo UI request

using KendoNET.DynamicLinq

[HttpPost]
public IActionResult Products([FromBody] DataSourceRequest requestModel)
{
    using (var northwind = new Northwind())
    {
        return northwind.Products
               .Select(p => new ProductViewModel // Use a view model to avoid serializing internal Entity Framework properties as JSON
               {
                   ProductID = p.ProductID,
                   ProductName = p.ProductName,
                   UnitPrice = p.UnitPrice,
                   UnitsInStock = p.UnitsInStock,
                   Discontinued = p.Discontinued
               })
               .ToDataSourceResult(requestModel.Take, requestModel.Skip, requestModel.Sort, requestModel.Filter, requestModel.Aggregate, requestModel.Group);
    }
}

Additional Configuration

The following configurations are optional. They can be omitted when the default Grid and server behavior is sufficient.

▸ Forward Column-Level ignoreCase to the Server

Use Filter.IgnoreCase to enable case-insensitive matching for eq, neq, contains, doesnotcontain, startswith, and endswith on string fields; leave it unset to keep the existing case-sensitive behavior. If you build DataSourceRequest/Filter manually instead of going through the Grid, just set this property directly.

When serverFiltering is enabled, the Grid column setting filterable.ignoreCase is not automatically included in the request sent to the server. Use parameterMap to copy the ignoreCase value from the column whose field matches the filter descriptor's field.

For example:

..... Other kendo grid code .....

dataSource: {
    schema: {...},
    transport: {
        read: {
            url: 'read url',
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            type: 'POST'
        },
        parameterMap: function (data, operation) {

            /* Add the following code to forward column-level ignoreCase to the server */
            var grid = $("#grid-id").data("kendoGrid");     // replace "grid-id" with your grid's id
            var columns = grid ? grid.columns : [];
            var pendingFilters = [];

            if (data.filter) { pendingFilters.push(data.filter); }
            while (pendingFilters.length > 0) {
                var filter = pendingFilters.pop();
                if (!filter) { continue; }
                if (filter.filters) {
                    for (var i = 0; i < filter.filters.length; i++) { pendingFilters.push(filter.filters[i]); }
                    continue;
                }

                var column = columns.find(function (col) { return col.field === filter.field; });
                if (column && column.filterable && typeof column.filterable === "object" && column.filterable.ignoreCase !== undefined)
                {
                    filter.ignoreCase = column.filterable.ignoreCase;
                }
            }

            // ... other parameterMap code ...

            return kendo.stringify(data);
        }
    },
    error: function(e) {...},
    pageSize: 20,
    serverPaging: true,
    serverFiltering: true,
    serverSorting: true,
    ...
}

..... Other kendo grid code .....

Known Issues

When server-side filterable options are enabled and apply a query with filter condition that contains DateTime type column, then EntityFramework Core would throw an exception System.Data.SqlClient.SqlException (0x80131904): Conversion failed when converting date and/or time from character string. The error is caused by a known issue in some old EntityFramework Core versions. The workaround is adding datetime value to the related column in DbContext. e.g.

public class MyContext : DbContext
{
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        ..........

        modelBuilder.Entity<Member>().Property(x => x.UpdateTime).HasColumnType("datetime");

        ..........
    }
}

How To Build NuGet Package

  1. Open command line console
  2. Switch to project root directory(src\KendoNET.DynamicLinq).
  3. Run "dotnet restore"
  4. Run "dotnet pack --configuration Release"
  5. Add <repository type="git" url="https://github.com/linmasaki/KendoNET.DynamicLinq.git" /> to package metadata of nupkg to show repository URL at Nuget

Note

  1. KendoNET.DynamicLinq is a reference to Ali Sarkis's Kendo.DynamicLinq.
  2. This package was previously published as Kendo.DynamicLinqCore. Due to a trademark concern, and following coordination with the trademark holder, the project was renamed to KendoNET.DynamicLinq. The old package has since been delisted from NuGet; please switch to the new package ID above.

Kendo UI Documentation

The following links are Kendo UI online docs(related to this package) and you can refer to.

More Kendo UI configuration can refer to here

About

Elevate Your Kendo UI Experience with this ASP.NET Core's Free Package!

Topics

Resources

Stars

47 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages