File tree Expand file tree Collapse file tree 3 files changed +39
-2
lines changed
DevExtreme.AspNet.Data.Tests Expand file tree Collapse file tree 3 files changed +39
-2
lines changed Original file line number Diff line number Diff 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 {
Original file line number Diff line number Diff 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 ) ;
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments