New to Telerik ReportingStart a free 30-day trial

Using Parameters with the EntityCoreDataSource Component

This article explains how to pass values from report parameters to a method or queryable property of a DbContext through the EntityCoreDataSource component. Parameter binding lets the report filter data on the database server rather than fetching the entire set into memory.

The EntityCoreDataSource wizard detects parameters declared on the data-retrieval method and prompts you to provide values for them on the Configure Data Source Parameters page.

Defining a Parameterized ContextMember

Add a method or queryable property on the DbContext that accepts the values you need to filter by. The names and types of the data source parameters must match the names and types of the corresponding method arguments exactly; otherwise the component throws an exception at runtime.

C#
public partial class AppDbContext : DbContext
{
    public IQueryable<Person> QueryPeopleWithSalaryAbove(decimal threshold)
    {
        return this.People.Where(p => p.Salary > threshold);
    }
}

Mapping Report Parameters to the Data Source

Set the ContextMember property to the method name and add one entry to the Parameters collection of the data source for every method argument. The Parameters collection is inherited through ObjectDataSourceBase and exposes the same fluent overload used by every Telerik Reporting data source: Add(name, type, valueOrExpression). Bind to a report parameter value with an expression such as =Parameters.threshold.Value:

C#
var dataSource = new Telerik.Reporting.EntityCoreDataSource
{
    Context = typeof(AppDbContext),
    ContextMember = "QueryPeopleWithSalaryAbove"
};

dataSource.Parameters.Add("threshold", typeof(decimal), "= Parameters.threshold.Value");

You can also pass literal values when the data source must execute with a fixed argument:

C#
dataSource.Parameters.Add("threshold", typeof(decimal), 60000);

Pushing Filtering to the Server

When the ContextMember returns an IQueryable<T>, EF Core translates the filter into SQL and executes it on the database. Avoid materializing the result with ToList() or ToArray() inside the DbContext method, because the entire set is then loaded into memory and the filter degrades to client-side evaluation, which negates the performance benefit of parameterized queries.

See Also