GridCustomDataSource.GetUniqueValuesAsync(GridCustomDataSourceUniqueValuesOptions, CancellationToken) Method
Returns an array of unique values contained in the specified column.
Namespace: DevExpress.Blazor
Assembly: DevExpress.Blazor.Grid.v26.1.dll
NuGet Package: DevExpress.Blazor
Declaration
public virtual Task<object[]> GetUniqueValuesAsync(
GridCustomDataSourceUniqueValuesOptions options,
CancellationToken cancellationToken
)
Parameters
| Name | Type | Description |
|---|---|---|
| options | GridCustomDataSourceUniqueValuesOptions | Options that data source items should match. |
| cancellationToken | CancellationToken | An object that propagates a cancellation notification. |
Returns
| Type | Description |
|---|---|
| Task<Object[]> | An asynchronous operation that returns unique column values. |
Remarks
If standard data binding modes do not meet specific business requirements, you can implement your own data provider. To do so, create a GridCustomDataSource descendant and assign it to the Grid or TreeList component’s Data property.
The following code snippet implements a custom data provider’s GetUniqueValuesAsync method:
using DevExpress.Blazor;
using DevExpress.Data.Filtering;
using DevExpress.Data.Filtering.Helpers;
using CustomDataSource.Models;
using System.Collections;
using System.Linq.Expressions;
using DevExpress.Data.Linq;
using DevExpress.Data.Linq.Helpers;
using Microsoft.EntityFrameworkCore;
namespace CustomDataSource.Services;
public class OrdersDataSource : GridCustomDataSource, IDisposable {
protected override Type DataItemType => typeof(Order);
private readonly NorthwindContext _context;
private bool _disposed = false;
public OrdersDataSource(IDbContextFactory<NorthwindContext> contextFactory) {
_context = contextFactory.CreateDbContext();
}
// ...
public override Task<object[]> GetUniqueValuesAsync(GridCustomDataSourceUniqueValuesOptions options, CancellationToken cancellationToken) {
var filteredQuery = ApplyFiltering(options.FilterCriteria, _context.Orders);
var lambda = GetTypedLambda(options.FieldName);
return filteredQuery
.Select(lambda)
.Distinct()
.OrderBy(x => x)
.ToArrayAsync(cancellationToken);
}
// ...
private static IQueryable<Order> ApplyFiltering(CriteriaOperator criteria, IQueryable<Order> queryableSource) {
return !criteria.ReferenceEqualsNull()
? (IQueryable<Order>)queryableSource.AppendWhere(
new CriteriaToEFExpressionConverter(queryableSource.Provider.GetType()), criteria)
: queryableSource;
}
// ...
private static Expression<Func<Order, object>> GetTypedLambda(string fieldName) {
var parameter = Expression.Parameter(typeof(Order));
var property = Expression.Property(parameter, fieldName);
var lambda = Expression.Lambda<Func<Order, object>>(Expression.Convert(property, typeof(object)), parameter);
return lambda;
}
// ...
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing) {
if (!_disposed) {
if (disposing) {
_context?.Dispose();
}
_disposed = true;
}
}
// ...
}
See Also