Skip to content

Commit 5235ff2

Browse files
SortByPrimaryKey option (#415)
1 parent 3167f39 commit 5235ff2

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

net/DevExtreme.AspNet.Data.Tests/DataSourceExpressionBuilderTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,39 @@ public void Build_Sorting() {
8686
Assert.Equal("data.OrderBy(obj => obj.Item1).ThenByDescending(obj => obj.Item2)", expr.ToString());
8787
}
8888

89+
[Fact]
90+
public void SortByPrimaryKey() {
91+
92+
void Case(Action<DataSourceLoadOptionsBase> initOptions, Action<string> assert) {
93+
var source = new[] {
94+
new { ID = 1, Value = "A" }
95+
};
96+
97+
var loadOptions = new SampleLoadOptions {
98+
GuardNulls = false,
99+
PrimaryKey = new[] { "ID" },
100+
SortByPrimaryKey = false
101+
};
102+
103+
initOptions?.Invoke(loadOptions);
104+
105+
assert(Compat.CreateDataSourceExpressionBuilder(source.AsQueryable(), loadOptions).BuildLoadExpr().ToString());
106+
}
107+
108+
Case(
109+
null,
110+
expr => Assert.DoesNotContain("OrderBy", expr)
111+
);
112+
113+
Case(
114+
options => options.DefaultSort = "Value",
115+
expr => {
116+
Assert.Contains(".OrderBy(obj => obj.Value)", expr);
117+
Assert.DoesNotContain("ThenBy", expr);
118+
}
119+
);
120+
}
121+
89122
[Fact]
90123
public void GroupingAddedToSorting() {
91124
var loadOptions = new SampleLoadOptions {

net/DevExtreme.AspNet.Data/DataSourceLoadContext.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ partial class DataSourceLoadContext {
138138
string[] _primaryKey;
139139
string _defaultSort;
140140

141-
public bool HasAnySort => HasGroups || HasSort || HasPrimaryKey || HasDefaultSort;
141+
public bool HasAnySort => HasGroups || HasSort || ShouldSortByPrimaryKey || HasDefaultSort;
142142

143143
bool HasSort => !IsEmpty(_options.Sort);
144144

@@ -160,6 +160,8 @@ string DefaultSort {
160160

161161
bool HasDefaultSort => !String.IsNullOrEmpty(DefaultSort);
162162

163+
bool ShouldSortByPrimaryKey => HasPrimaryKey && _options.SortByPrimaryKey.GetValueOrDefault(true);
164+
163165
public IEnumerable<SortingInfo> GetFullSort() {
164166
var memo = new HashSet<string>();
165167
var result = new List<SortingInfo>();
@@ -189,7 +191,7 @@ public IEnumerable<SortingInfo> GetFullSort() {
189191
if(HasDefaultSort)
190192
requiredSort = requiredSort.Concat(new[] { DefaultSort });
191193

192-
if(HasPrimaryKey)
194+
if(ShouldSortByPrimaryKey)
193195
requiredSort = requiredSort.Concat(PrimaryKey);
194196

195197
return Utils.AddRequiredSort(result, requiredSort);

net/DevExtreme.AspNet.Data/DataSourceLoadOptionsBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ public class DataSourceLoadOptionsBase {
110110
/// </summary>
111111
public bool? PaginateViaPrimaryKey { get; set; }
112112

113+
public bool? SortByPrimaryKey { get; set; }
114+
113115
public bool AllowAsyncOverSync { get; set; }
114116

115117
#if DEBUG

0 commit comments

Comments
 (0)