Skip to content

Commit 9d489d2

Browse files
authored
RestrictOrderByToPropertyOrField (#857)
* RestrictOrderBy * , * 3 * Entities_OrderBy_RestrictOrderByIsFalse * Skip * .
1 parent dcf26d6 commit 9d489d2

14 files changed

+348
-83
lines changed

System.Linq.Dynamic.Core.sln.DotSettings

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
22
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=EF/@EntryIndexedValue">EF</s:String>
3+
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=IIF/@EntryIndexedValue">IIF</s:String>
34
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=IL/@EntryIndexedValue">IL</s:String>
45
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=UTC/@EntryIndexedValue">UTC</s:String>
56
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=WASM/@EntryIndexedValue">WASM</s:String>

src/System.Linq.Dynamic.Core/AnyOfTypes/AnyOfTypes.g.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ namespace AnyOfTypes
1111
{
1212
internal enum AnyOfType
1313
{
14-
Undefined = 0, First, Second
14+
Undefined = 0, First, Second, Third
1515
}
1616
}

src/System.Linq.Dynamic.Core/AnyOfTypes/AnyOf_2.g.cs

+8-6
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77
// </auto-generated>
88
//------------------------------------------------------------------------------
99

10+
#pragma warning disable CS1591
11+
1012
using System;
1113
using System.Diagnostics;
1214
using System.Collections.Generic;
1315

1416
namespace AnyOfTypes
1517
{
1618
[DebuggerDisplay("{_thisType}, AnyOfType = {_currentType}; Type = {_currentValueType?.Name}; Value = '{ToString()}'")]
17-
internal struct AnyOf<TFirst, TSecond>
19+
internal struct AnyOf<TFirst, TSecond> : IEquatable<AnyOf<TFirst, TSecond>>
1820
{
1921
private readonly string _thisType => $"AnyOf<{typeof(TFirst).Name}, {typeof(TSecond).Name}>";
2022
private readonly int _numberOfTypes;
@@ -124,23 +126,23 @@ public override int GetHashCode()
124126
return HashCodeCalculator.GetHashCode(fields);
125127
}
126128

127-
private bool Equals(AnyOf<TFirst, TSecond> other)
129+
public bool Equals(AnyOf<TFirst, TSecond> other)
128130
{
129131
return _currentType == other._currentType &&
130132
_numberOfTypes == other._numberOfTypes &&
131133
EqualityComparer<object>.Default.Equals(_currentValue, other._currentValue) &&
132-
EqualityComparer<TFirst>.Default.Equals(_first, other._first) &&
133-
EqualityComparer<TSecond>.Default.Equals(_second, other._second);
134+
EqualityComparer<TFirst>.Default.Equals(_first, other._first) &&
135+
EqualityComparer<TSecond>.Default.Equals(_second, other._second);
134136
}
135137

136138
public static bool operator ==(AnyOf<TFirst, TSecond> obj1, AnyOf<TFirst, TSecond> obj2)
137139
{
138-
return obj1.Equals(obj2);
140+
return EqualityComparer<AnyOf<TFirst, TSecond>>.Default.Equals(obj1, obj2);
139141
}
140142

141143
public static bool operator !=(AnyOf<TFirst, TSecond> obj1, AnyOf<TFirst, TSecond> obj2)
142144
{
143-
return !obj1.Equals(obj2);
145+
return !(obj1 == obj2);
144146
}
145147

146148
public override bool Equals(object obj)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by https://github.com/StefH/AnyOf.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
#pragma warning disable CS1591
11+
12+
using System;
13+
using System.Diagnostics;
14+
using System.Collections.Generic;
15+
16+
namespace AnyOfTypes
17+
{
18+
[DebuggerDisplay("{_thisType}, AnyOfType = {_currentType}; Type = {_currentValueType?.Name}; Value = '{ToString()}'")]
19+
internal struct AnyOf<TFirst, TSecond, TThird> : IEquatable<AnyOf<TFirst, TSecond, TThird>>
20+
{
21+
private readonly string _thisType => $"AnyOf<{typeof(TFirst).Name}, {typeof(TSecond).Name}, {typeof(TThird).Name}>";
22+
private readonly int _numberOfTypes;
23+
private readonly object _currentValue;
24+
private readonly Type _currentValueType;
25+
private readonly AnyOfType _currentType;
26+
27+
private readonly TFirst _first;
28+
private readonly TSecond _second;
29+
private readonly TThird _third;
30+
31+
public readonly AnyOfType[] AnyOfTypes => new[] { AnyOfType.First, AnyOfType.Second, AnyOfType.Third };
32+
public readonly Type[] Types => new[] { typeof(TFirst), typeof(TSecond), typeof(TThird) };
33+
public bool IsUndefined => _currentType == AnyOfType.Undefined;
34+
public bool IsFirst => _currentType == AnyOfType.First;
35+
public bool IsSecond => _currentType == AnyOfType.Second;
36+
public bool IsThird => _currentType == AnyOfType.Third;
37+
38+
public static implicit operator AnyOf<TFirst, TSecond, TThird>(TFirst value) => new AnyOf<TFirst, TSecond, TThird>(value);
39+
40+
public static implicit operator TFirst(AnyOf<TFirst, TSecond, TThird> @this) => @this.First;
41+
42+
public AnyOf(TFirst value)
43+
{
44+
_numberOfTypes = 3;
45+
_currentType = AnyOfType.First;
46+
_currentValue = value;
47+
_currentValueType = typeof(TFirst);
48+
_first = value;
49+
_second = default;
50+
_third = default;
51+
}
52+
53+
public TFirst First
54+
{
55+
get
56+
{
57+
Validate(AnyOfType.First);
58+
return _first;
59+
}
60+
}
61+
62+
public static implicit operator AnyOf<TFirst, TSecond, TThird>(TSecond value) => new AnyOf<TFirst, TSecond, TThird>(value);
63+
64+
public static implicit operator TSecond(AnyOf<TFirst, TSecond, TThird> @this) => @this.Second;
65+
66+
public AnyOf(TSecond value)
67+
{
68+
_numberOfTypes = 3;
69+
_currentType = AnyOfType.Second;
70+
_currentValue = value;
71+
_currentValueType = typeof(TSecond);
72+
_second = value;
73+
_first = default;
74+
_third = default;
75+
}
76+
77+
public TSecond Second
78+
{
79+
get
80+
{
81+
Validate(AnyOfType.Second);
82+
return _second;
83+
}
84+
}
85+
86+
public static implicit operator AnyOf<TFirst, TSecond, TThird>(TThird value) => new AnyOf<TFirst, TSecond, TThird>(value);
87+
88+
public static implicit operator TThird(AnyOf<TFirst, TSecond, TThird> @this) => @this.Third;
89+
90+
public AnyOf(TThird value)
91+
{
92+
_numberOfTypes = 3;
93+
_currentType = AnyOfType.Third;
94+
_currentValue = value;
95+
_currentValueType = typeof(TThird);
96+
_third = value;
97+
_first = default;
98+
_second = default;
99+
}
100+
101+
public TThird Third
102+
{
103+
get
104+
{
105+
Validate(AnyOfType.Third);
106+
return _third;
107+
}
108+
}
109+
110+
private void Validate(AnyOfType desiredType)
111+
{
112+
if (desiredType != _currentType)
113+
{
114+
throw new InvalidOperationException($"Attempting to get {desiredType} when {_currentType} is set");
115+
}
116+
}
117+
118+
public AnyOfType CurrentType
119+
{
120+
get
121+
{
122+
return _currentType;
123+
}
124+
}
125+
126+
public object CurrentValue
127+
{
128+
get
129+
{
130+
return _currentValue;
131+
}
132+
}
133+
134+
public Type CurrentValueType
135+
{
136+
get
137+
{
138+
return _currentValueType;
139+
}
140+
}
141+
142+
public override int GetHashCode()
143+
{
144+
var fields = new object[]
145+
{
146+
_numberOfTypes,
147+
_currentValue,
148+
_currentType,
149+
_first,
150+
_second,
151+
_third,
152+
typeof(TFirst),
153+
typeof(TSecond),
154+
typeof(TThird),
155+
};
156+
return HashCodeCalculator.GetHashCode(fields);
157+
}
158+
159+
public bool Equals(AnyOf<TFirst, TSecond, TThird> other)
160+
{
161+
return _currentType == other._currentType &&
162+
_numberOfTypes == other._numberOfTypes &&
163+
EqualityComparer<object>.Default.Equals(_currentValue, other._currentValue) &&
164+
EqualityComparer<TFirst>.Default.Equals(_first, other._first) &&
165+
EqualityComparer<TSecond>.Default.Equals(_second, other._second) &&
166+
EqualityComparer<TThird>.Default.Equals(_third, other._third);
167+
}
168+
169+
public static bool operator ==(AnyOf<TFirst, TSecond, TThird> obj1, AnyOf<TFirst, TSecond, TThird> obj2)
170+
{
171+
return EqualityComparer<AnyOf<TFirst, TSecond, TThird>>.Default.Equals(obj1, obj2);
172+
}
173+
174+
public static bool operator !=(AnyOf<TFirst, TSecond, TThird> obj1, AnyOf<TFirst, TSecond, TThird> obj2)
175+
{
176+
return !(obj1 == obj2);
177+
}
178+
179+
public override bool Equals(object obj)
180+
{
181+
return obj is AnyOf<TFirst, TSecond, TThird> o && Equals(o);
182+
}
183+
184+
public override string ToString()
185+
{
186+
return IsUndefined ? null : $"{_currentValue}";
187+
}
188+
}
189+
}

src/System.Linq.Dynamic.Core/DynamicQueryableExtensions.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1564,21 +1564,21 @@ internal static IOrderedQueryable InternalOrderBy(IQueryable source, ParsingConf
15641564
{
15651565
Check.NotNull(source);
15661566
Check.NotNull(config);
1567-
Check.NotEmpty(ordering, nameof(ordering));
1567+
Check.NotEmpty(ordering);
15681568

1569-
ParameterExpression[] parameters = { ParameterExpressionHelper.CreateParameterExpression(source.ElementType, string.Empty, config.RenameEmptyParameterExpressionNames) };
1570-
ExpressionParser parser = new ExpressionParser(parameters, ordering, args, config);
1571-
IList<DynamicOrdering> dynamicOrderings = parser.ParseOrdering();
1569+
ParameterExpression[] parameters = [ParameterExpressionHelper.CreateParameterExpression(source.ElementType, string.Empty, config.RenameEmptyParameterExpressionNames)];
1570+
var parser = new ExpressionParser(parameters, ordering, args, config, true);
1571+
var dynamicOrderings = parser.ParseOrdering();
15721572

1573-
Expression queryExpr = source.Expression;
1573+
var queryExpr = source.Expression;
15741574

1575-
foreach (DynamicOrdering dynamicOrdering in dynamicOrderings)
1575+
foreach (var dynamicOrdering in dynamicOrderings)
15761576
{
15771577
if (comparer == null)
15781578
{
15791579
queryExpr = Expression.Call(
15801580
typeof(Queryable), dynamicOrdering.MethodName,
1581-
new[] { source.ElementType, dynamicOrdering.Selector.Type },
1581+
[source.ElementType, dynamicOrdering.Selector.Type],
15821582
queryExpr, Expression.Quote(Expression.Lambda(dynamicOrdering.Selector, parameters)));
15831583
}
15841584
else
@@ -1602,7 +1602,7 @@ internal static IOrderedQueryable InternalOrderBy(IQueryable source, ParsingConf
16021602

16031603
queryExpr = Expression.Call(
16041604
typeof(Queryable), dynamicOrdering.MethodName,
1605-
new[] { source.ElementType, dynamicOrdering.Selector.Type },
1605+
[source.ElementType, dynamicOrdering.Selector.Type],
16061606
queryExpr, Expression.Quote(Expression.Lambda(dynamicOrdering.Selector, parameters)),
16071607
constant);
16081608
}

0 commit comments

Comments
 (0)