Skip to content

Commit c8841b0

Browse files
Merge pull request #380 from Lempireqc/master
save
2 parents 07d37de + c3a6af5 commit c8841b0

6 files changed

+223
-8
lines changed

System.Linq.Dynamic.Core.sln

+21
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docfx", "docfx", "{012536E6
9090
docfx\toc.yml = docfx\toc.yml
9191
EndProjectSection
9292
EndProject
93+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "lab", "lab", "{E97833C1-77B6-44E2-8793-C1F952CA936F}"
94+
EndProject
95+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Z.Dynamic.Core.Lab", "Z.Dynamic.Core.Lab\Z.Dynamic.Core.Lab.csproj", "{CDD8D5BF-A212-43DD-B043-4B7242C553E0}"
96+
EndProject
9397
Global
9498
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9599
Debug|Any CPU = Debug|Any CPU
@@ -504,6 +508,22 @@ Global
504508
{CD80A3AC-B0E1-45ED-BE07-DE6A0F1D4CD8}.Release|x64.Build.0 = Release|Any CPU
505509
{CD80A3AC-B0E1-45ED-BE07-DE6A0F1D4CD8}.Release|x86.ActiveCfg = Release|Any CPU
506510
{CD80A3AC-B0E1-45ED-BE07-DE6A0F1D4CD8}.Release|x86.Build.0 = Release|Any CPU
511+
{CDD8D5BF-A212-43DD-B043-4B7242C553E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
512+
{CDD8D5BF-A212-43DD-B043-4B7242C553E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
513+
{CDD8D5BF-A212-43DD-B043-4B7242C553E0}.Debug|ARM.ActiveCfg = Debug|Any CPU
514+
{CDD8D5BF-A212-43DD-B043-4B7242C553E0}.Debug|ARM.Build.0 = Debug|Any CPU
515+
{CDD8D5BF-A212-43DD-B043-4B7242C553E0}.Debug|x64.ActiveCfg = Debug|Any CPU
516+
{CDD8D5BF-A212-43DD-B043-4B7242C553E0}.Debug|x64.Build.0 = Debug|Any CPU
517+
{CDD8D5BF-A212-43DD-B043-4B7242C553E0}.Debug|x86.ActiveCfg = Debug|Any CPU
518+
{CDD8D5BF-A212-43DD-B043-4B7242C553E0}.Debug|x86.Build.0 = Debug|Any CPU
519+
{CDD8D5BF-A212-43DD-B043-4B7242C553E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
520+
{CDD8D5BF-A212-43DD-B043-4B7242C553E0}.Release|Any CPU.Build.0 = Release|Any CPU
521+
{CDD8D5BF-A212-43DD-B043-4B7242C553E0}.Release|ARM.ActiveCfg = Release|Any CPU
522+
{CDD8D5BF-A212-43DD-B043-4B7242C553E0}.Release|ARM.Build.0 = Release|Any CPU
523+
{CDD8D5BF-A212-43DD-B043-4B7242C553E0}.Release|x64.ActiveCfg = Release|Any CPU
524+
{CDD8D5BF-A212-43DD-B043-4B7242C553E0}.Release|x64.Build.0 = Release|Any CPU
525+
{CDD8D5BF-A212-43DD-B043-4B7242C553E0}.Release|x86.ActiveCfg = Release|Any CPU
526+
{CDD8D5BF-A212-43DD-B043-4B7242C553E0}.Release|x86.Build.0 = Release|Any CPU
507527
EndGlobalSection
508528
GlobalSection(SolutionProperties) = preSolution
509529
HideSolutionNode = FALSE
@@ -534,6 +554,7 @@ Global
534554
{D160E2CF-A7E1-4DDE-9AB8-8CFB0087DCEB} = {7971CAEB-B9F2-416B-966D-2D697C4C1E62}
535555
{0034821E-740D-4553-821B-14CE9213C43C} = {7971CAEB-B9F2-416B-966D-2D697C4C1E62}
536556
{CD80A3AC-B0E1-45ED-BE07-DE6A0F1D4CD8} = {122BC4FA-7563-4E35-9D17-077F16F1629F}
557+
{CDD8D5BF-A212-43DD-B043-4B7242C553E0} = {E97833C1-77B6-44E2-8793-C1F952CA936F}
537558
EndGlobalSection
538559
GlobalSection(ExtensibilityGlobals) = postSolution
539560
SolutionGuid = {94C56722-194E-4B8B-BC23-B3F754E89A20}

Z.Dynamic.Core.Lab/Program.cs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace Z.Dynamic.Core.Lab
4+
{
5+
class Program
6+
{
7+
static void Main(string[] args)
8+
{
9+
Request_OrderBy_StringComparer.Execute();
10+
}
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.Linq;
5+
using System.Linq.Dynamic.Core;
6+
using System.Text;
7+
8+
namespace Z.Dynamic.Core.Lab
9+
{
10+
class Request_OrderBy_StringComparer
11+
{
12+
public class Customer
13+
{
14+
public string City { get; set; }
15+
public List<Order> Orders { get; set; }
16+
public string CompanyName { get; set; }
17+
public string Phone { get; set; }
18+
}
19+
public class Order
20+
{
21+
}
22+
23+
public static void Execute()
24+
{
25+
List<Customer> customers = new List<Customer>() { new Customer() { CompanyName = "Ååå"} ,
26+
new Customer() { CompanyName = "Bbb" } ,
27+
new Customer() { CompanyName = "Ååå" } ,
28+
new Customer() { CompanyName = "Bbb" } ,
29+
new Customer() { CompanyName = "Aaa" },
30+
new Customer() { CompanyName = "Aaa" },
31+
};
32+
33+
CultureInfo culture = new CultureInfo("nb-NO");
34+
var test1 = customers.AsQueryable().OrderBy(x => x.CompanyName, StringComparer.Create(culture, true)).ToList();
35+
var test2 = customers.AsQueryable().OrderBy(x => x.CompanyName).ToList();
36+
var test3 = customers.AsQueryable()
37+
.OrderBy("City").ThenBy("CompanyName", StringComparer.Create(culture, true)).ToDynamicList();
38+
var test4 = customers.AsQueryable()
39+
.OrderBy("CompanyName", StringComparer.Create(culture, true)).ToDynamicList();
40+
}
41+
}
42+
}

Z.Dynamic.Core.Lab/Template_Simple.cs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Linq.Dynamic.Core;
6+
7+
namespace Z.Dynamic.Core.Lab
8+
{
9+
public class Template_Simple
10+
{
11+
public class Customer
12+
{
13+
public string City { get; set; }
14+
public List<Order> Orders { get; set; }
15+
public string CompanyName { get; set; }
16+
public string Phone { get; set; }
17+
}
18+
public class Order
19+
{
20+
}
21+
22+
public static void Execute()
23+
{
24+
List<Customer> customers = new List<Customer>() {new Customer() {City = "ZZZ", CompanyName = "ZZZ", Orders = new List<Order>() {new Order()}, Phone = "555 5555"}};
25+
26+
var query = customers.AsQueryable()
27+
.Where("City == @0 and Orders.Count >= @1", "ZZZ", 1)
28+
.OrderBy("CompanyName")
29+
.Select("new(CompanyName as Name, Phone)").ToDynamicList();
30+
}
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="System.Reflection.Emit" Version="4.3.0" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<ProjectReference Include="..\src\System.Linq.Dynamic.Core\System.Linq.Dynamic.Core.csproj" />
14+
</ItemGroup>
15+
16+
</Project>

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

+100-8
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,35 @@ public static IOrderedQueryable<TSource> OrderBy<TSource>([NotNull] this IQuerya
12731273
return OrderBy(source, ParsingConfig.Default, ordering, args);
12741274
}
12751275

1276+
/// <summary>
1277+
/// Sorts the elements of a sequence in ascending or descending order according to a key.
1278+
/// </summary>
1279+
/// <typeparam name="TSource">The type of the elements of source.</typeparam>
1280+
/// <param name="source">A sequence of values to order.</param>
1281+
/// <param name="config">The <see cref="ParsingConfig"/>.</param>
1282+
/// <param name="ordering">An expression string to indicate values to order by.</param>
1283+
/// <param name="comparer">The comparer to use to order by.</param>
1284+
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
1285+
/// <returns>A <see cref="IQueryable{T}"/> whose elements are sorted according to the specified <paramref name="ordering"/>.</returns>
1286+
public static IOrderedQueryable<TSource> OrderBy<TSource>([NotNull] this IQueryable<TSource> source, [NotNull] ParsingConfig config, [NotNull] string ordering, IComparer comparer, params object[] args)
1287+
{
1288+
return (IOrderedQueryable<TSource>)InternalOrderBy((IQueryable)source, config, ordering, comparer, args);
1289+
}
1290+
1291+
/// <summary>
1292+
/// Sorts the elements of a sequence in ascending or descending order according to a key.
1293+
/// </summary>
1294+
/// <typeparam name="TSource">The type of the elements of source.</typeparam>
1295+
/// <param name="source">A sequence of values to order.</param>
1296+
/// <param name="ordering">An expression string to indicate values to order by.</param>
1297+
/// <param name="comparer">The comparer to use to order by.</param>
1298+
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
1299+
/// <returns>A <see cref="IQueryable{T}"/> whose elements are sorted according to the specified <paramref name="ordering"/>.</returns>
1300+
public static IOrderedQueryable<TSource> OrderBy<TSource>([NotNull] this IQueryable<TSource> source, [NotNull] string ordering, IComparer comparer, params object[] args)
1301+
{
1302+
return OrderBy(source, ParsingConfig.Default, ordering, comparer, args);
1303+
}
1304+
12761305
/// <summary>
12771306
/// Sorts the elements of a sequence in ascending or descending order according to a key.
12781307
/// </summary>
@@ -1289,6 +1318,11 @@ public static IOrderedQueryable<TSource> OrderBy<TSource>([NotNull] this IQuerya
12891318
/// </code>
12901319
/// </example>
12911320
public static IOrderedQueryable OrderBy([NotNull] this IQueryable source, [NotNull] ParsingConfig config, [NotNull] string ordering, params object[] args)
1321+
{
1322+
return InternalOrderBy(source, config, ordering, null, args);
1323+
}
1324+
1325+
internal static IOrderedQueryable InternalOrderBy([NotNull] IQueryable source, [NotNull] ParsingConfig config, [NotNull] string ordering, IComparer comparer, params object[] args)
12921326
{
12931327
Check.NotNull(source, nameof(source));
12941328
Check.NotNull(config, nameof(config));
@@ -1302,10 +1336,22 @@ public static IOrderedQueryable OrderBy([NotNull] this IQueryable source, [NotNu
13021336

13031337
foreach (DynamicOrdering dynamicOrdering in dynamicOrderings)
13041338
{
1305-
queryExpr = Expression.Call(
1306-
typeof(Queryable), dynamicOrdering.MethodName,
1307-
new[] { source.ElementType, dynamicOrdering.Selector.Type },
1308-
queryExpr, Expression.Quote(Expression.Lambda(dynamicOrdering.Selector, parameters)));
1339+
if (comparer == null)
1340+
{
1341+
queryExpr = Expression.Call(
1342+
typeof(Queryable), dynamicOrdering.MethodName,
1343+
new[] { source.ElementType, dynamicOrdering.Selector.Type },
1344+
queryExpr, Expression.Quote(Expression.Lambda(dynamicOrdering.Selector, parameters)));
1345+
}
1346+
else
1347+
{
1348+
var comparerGenericType = typeof(IComparer<>).MakeGenericType(dynamicOrdering.Selector.Type);
1349+
queryExpr = Expression.Call(
1350+
typeof(Queryable), dynamicOrdering.MethodName,
1351+
new[] { source.ElementType, dynamicOrdering.Selector.Type },
1352+
queryExpr, Expression.Quote(Expression.Lambda(dynamicOrdering.Selector, parameters)),
1353+
Expression.Constant(comparer, comparerGenericType));
1354+
}
13091355
}
13101356

13111357
var optimized = OptimizeExpression(queryExpr);
@@ -2176,6 +2222,35 @@ public static IOrderedQueryable<TSource> ThenBy<TSource>([NotNull] this IOrdered
21762222
{
21772223
return ThenBy(source, ParsingConfig.Default, ordering, args);
21782224
}
2225+
2226+
/// <summary>
2227+
/// Performs a subsequent ordering of the elements in a sequence in ascending order according to a key.
2228+
/// </summary>
2229+
/// <typeparam name="TSource">The type of the elements of source.</typeparam>
2230+
/// <param name="source">A sequence of values to order.</param>
2231+
/// <param name="config">The <see cref="ParsingConfig"/>.</param>
2232+
/// <param name="ordering">An expression string to indicate values to order by.</param>
2233+
/// <param name="comparer">The comparer to use to order by.</param>
2234+
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
2235+
/// <returns>A <see cref="IOrderedQueryable{T}"/> whose elements are sorted according to the specified <paramref name="ordering"/>.</returns>
2236+
public static IOrderedQueryable<TSource> ThenBy<TSource>([NotNull] this IOrderedQueryable<TSource> source, [NotNull] ParsingConfig config, [NotNull] string ordering, IComparer comparer, params object[] args)
2237+
{
2238+
return (IOrderedQueryable<TSource>)InternalThenBy((IOrderedQueryable)source, config, ordering, comparer, args);
2239+
}
2240+
2241+
/// <summary>
2242+
/// Performs a subsequent ordering of the elements in a sequence in ascending order according to a key.
2243+
/// </summary>
2244+
/// <typeparam name="TSource">The type of the elements of source.</typeparam>
2245+
/// <param name="source">A sequence of values to order.</param>
2246+
/// <param name="ordering">An expression string to indicate values to order by.</param>
2247+
/// <param name="comparer">The comparer to use to order by.</param>
2248+
/// <param name="args">An object array that contains zero or more objects to insert into the predicate as parameters. Similar to the way String.Format formats strings.</param>
2249+
/// <returns>A <see cref="IOrderedQueryable{T}"/> whose elements are sorted according to the specified <paramref name="ordering"/>.</returns>
2250+
public static IOrderedQueryable<TSource> ThenBy<TSource>([NotNull] this IOrderedQueryable<TSource> source, [NotNull] string ordering, IComparer comparer, params object[] args)
2251+
{
2252+
return ThenBy(source, ParsingConfig.Default, ordering, comparer, args);
2253+
}
21792254
/// <summary>
21802255
/// Performs a subsequent ordering of the elements in a sequence in ascending order according to a key.
21812256
/// </summary>
@@ -2193,6 +2268,11 @@ public static IOrderedQueryable<TSource> ThenBy<TSource>([NotNull] this IOrdered
21932268
/// </code>
21942269
/// </example>
21952270
public static IOrderedQueryable ThenBy([NotNull] this IOrderedQueryable source, [NotNull] ParsingConfig config, [NotNull] string ordering, params object[] args)
2271+
{
2272+
return InternalThenBy(source, config, ordering, null, args);
2273+
}
2274+
2275+
internal static IOrderedQueryable InternalThenBy([NotNull] this IOrderedQueryable source, [NotNull] ParsingConfig config, [NotNull] string ordering, IComparer comparer, params object[] args)
21962276
{
21972277
Check.NotNull(source, nameof(source));
21982278
Check.NotNull(config, nameof(config));
@@ -2206,10 +2286,22 @@ public static IOrderedQueryable ThenBy([NotNull] this IOrderedQueryable source,
22062286

22072287
foreach (DynamicOrdering dynamicOrdering in dynamicOrderings)
22082288
{
2209-
queryExpr = Expression.Call(
2210-
typeof(Queryable), dynamicOrdering.MethodName,
2211-
new[] { source.ElementType, dynamicOrdering.Selector.Type },
2212-
queryExpr, Expression.Quote(Expression.Lambda(dynamicOrdering.Selector, parameters)));
2289+
if (comparer == null)
2290+
{
2291+
queryExpr = Expression.Call(
2292+
typeof(Queryable), dynamicOrdering.MethodName,
2293+
new[] { source.ElementType, dynamicOrdering.Selector.Type },
2294+
queryExpr, Expression.Quote(Expression.Lambda(dynamicOrdering.Selector, parameters)));
2295+
}
2296+
else
2297+
{
2298+
var comparerGenericType = typeof(IComparer<>).MakeGenericType(dynamicOrdering.Selector.Type);
2299+
queryExpr = Expression.Call(
2300+
typeof(Queryable), dynamicOrdering.MethodName,
2301+
new[] { source.ElementType, dynamicOrdering.Selector.Type },
2302+
queryExpr, Expression.Quote(Expression.Lambda(dynamicOrdering.Selector, parameters)),
2303+
Expression.Constant(comparer, comparerGenericType));
2304+
}
22132305
}
22142306

22152307
var optimized = OptimizeExpression(queryExpr);

0 commit comments

Comments
 (0)