Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,39 @@ string BuildExpr(DataSourceLoadOptionsBase options) => Compat.CreateDataSourceEx
})
);
}

[Fact]
public void BuildGroupCountExpr_IntervalGrouping() {
string BuildExpr(DataSourceLoadOptionsBase options) => Compat.CreateDataSourceExpressionBuilder<Tuple<int, DateTime>>(options)
.BuildGroupCountExpr()
.ToString();

Assert.Equal(
"data.Where(obj => (obj.Item1 == 1))" +
// "Convert" expression is displayed differently in the .NET Framework and .NET Core
// https://github.com/microsoft/referencesource/blob/ec9fa9ae770d522a5b5f0607898044b7478574a3/System.Core/Microsoft/Scripting/Ast/ExpressionStringBuilder.cs#L657
#if NET4
".GroupBy(obj => new AnonType`1(I0 = Convert(obj.Item2.Year)))" +
#else
".GroupBy(obj => new AnonType`1(I0 = Convert(obj.Item2.Year, Nullable`1)))" +
#endif
".OrderBy(g => g.Key.I0)" +
".Select(g => new AnonType`2(I0 = g.Count(), I1 = g.Key.I0))" +
".Count()",

BuildExpr(new SampleLoadOptions {
GuardNulls = false,
Filter = new[] { "Item1", "1" },
RequireGroupCount = true,
Group = new[] {
new GroupingInfo { Selector = "Item2", GroupInterval = "year" }
},
GroupSummary = new[] {
new SummaryInfo { Selector = "Item2", SummaryType = "max" }
}
})
);
}
}

}
21 changes: 21 additions & 0 deletions net/DevExtreme.AspNet.Data.Tests/RemoteGroupingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,27 @@ public void RequireGroupCount_MultiLevels() {
Assert.Equal(2, loadResult.groupCount);
}

[Fact]
public void RequireGroupCount_Intervals() {
var source = new[] {
new { G1 = new DateTime(2025, 1, 1), PK = 0 },
new { G1 = new DateTime(2025, 2, 2), PK = 1 },
new { G1 = new DateTime(2026, 3, 3), PK = 2 }
};

var loadOptions = new SampleLoadOptions {
RemoteGrouping = true,
RequireGroupCount = true,
Group = new[] {
new GroupingInfo { Selector = "G1", IsExpanded = false, GroupInterval = "year" },
},
Skip = 1
};

var loadResult = DataSourceLoader.Load(source, loadOptions);
Assert.Equal(2, loadResult.groupCount);
}

[Fact]
public void Summary_MissingOverload() {
// Neither of Min, Max, Sum, Average provides an overload for byte sequences
Expand Down
17 changes: 12 additions & 5 deletions net/DevExtreme.AspNet.Data/DataSourceExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,23 @@ public Expression BuildCountExpr() {

public Expression BuildLoadGroupsExpr(bool paginate, bool suppressGroups = false, bool suppressTotals = false) {
AddFilter();
AddRemoteGrouping(suppressGroups, suppressTotals);
AddRemoteGrouping(suppressGroups, suppressTotals, suppressGroups);
if(paginate)
AddPaging();
return Expr;
}

public Expression BuildGroupCountExpr() {
AddFilter();
Expr = CreateSelectCompiler().CompileSingle(Expr, Context.Group.Single().Selector);
Expr = QueryableCall(nameof(Queryable.Distinct));

var group = Context.Group.Single();
if(String.IsNullOrEmpty(group.GroupInterval)) {
Expr = CreateSelectCompiler().CompileSingle(Expr, group.Selector);
Expr = QueryableCall(nameof(Queryable.Distinct));
} else {
AddRemoteGrouping(false, true, true);
}

AddCount();
return Expr;
}
Expand Down Expand Up @@ -75,12 +82,12 @@ void AddPaging() {
Expr = QueryableCall(nameof(Queryable.Take), Expression.Constant(Context.Take));
}

void AddRemoteGrouping(bool suppressGroups, bool suppressTotals) {
void AddRemoteGrouping(bool suppressGroups, bool suppressTotals, bool suppressGroupSummary) {
var compiler = new RemoteGroupExpressionCompiler(
GetItemType(), Context.GuardNulls, Context.ExpandLinqSumType, Context.CreateAnonTypeNewTweaks(),
suppressGroups ? null : Context.Group,
suppressTotals ? null : Context.TotalSummary,
suppressGroups ? null : Context.GroupSummary
suppressGroupSummary ? null : Context.GroupSummary
);
Expr = compiler.Compile(Expr);
}
Expand Down
Loading