Skip to content

Commit 1e0ed4e

Browse files
authored
Added EvaluateGroupByAtDatabase For EF Core 2.1 (#197)
* EvaluateGroupByAtDatabase (#165) * wip * Added ParsingConfig.DefaultEFCore21 * Added EvaluateGroupByAtDatabase to Select and SelectMany
1 parent c730a78 commit 1e0ed4e

File tree

12 files changed

+259
-23
lines changed

12 files changed

+259
-23
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ The following frameworks are supported:
4848
- net45 and up
4949
- netstandard1.3
5050
- netstandard2.0
51-
- uap10.0
51+
- uap10.0 (TODO...)s
5252

5353
## Fork details
5454
This fork takes the basic library to a new level. Contains XML Documentation and examples on how to use it. Also adds unit testing to help ensure that it works properly.

System.Linq.Dynamic.Core.sln

+19
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp_netcore2.0_EF2.0
5353
EndProject
5454
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MemoryLeakTest167", "src-console\MemoryLeakTest167\MemoryLeakTest167.csproj", "{AD4F83E5-4240-485D-BB5C-F43974F716E4}"
5555
EndProject
56+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp_netcore2.0_EF2.1", "src-console\ConsoleAppEF2.1\ConsoleApp_netcore2.0_EF2.1.csproj", "{EDF434F6-70C0-4005-B63E-0C365B3DA42A}"
57+
EndProject
5658
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp_netcore2.1_EF2.1.1_InMemory", "src-console\ConsoleAppEF2.1.1_InMemory\ConsoleApp_netcore2.1_EF2.1.1_InMemory.csproj", "{E4286D1D-A98C-4516-9E47-CBF9739448DA}"
5759
EndProject
5860
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp_netcore2.1_EF2.1.1", "src-console\ConsoleAppEF2.1.1\ConsoleApp_netcore2.1_EF2.1.1.csproj", "{F1880F07-238F-4A3A-9E58-141350665E1F}"
@@ -291,6 +293,22 @@ Global
291293
{AD4F83E5-4240-485D-BB5C-F43974F716E4}.Release|x64.Build.0 = Release|Any CPU
292294
{AD4F83E5-4240-485D-BB5C-F43974F716E4}.Release|x86.ActiveCfg = Release|Any CPU
293295
{AD4F83E5-4240-485D-BB5C-F43974F716E4}.Release|x86.Build.0 = Release|Any CPU
296+
{EDF434F6-70C0-4005-B63E-0C365B3DA42A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
297+
{EDF434F6-70C0-4005-B63E-0C365B3DA42A}.Debug|Any CPU.Build.0 = Debug|Any CPU
298+
{EDF434F6-70C0-4005-B63E-0C365B3DA42A}.Debug|ARM.ActiveCfg = Debug|Any CPU
299+
{EDF434F6-70C0-4005-B63E-0C365B3DA42A}.Debug|ARM.Build.0 = Debug|Any CPU
300+
{EDF434F6-70C0-4005-B63E-0C365B3DA42A}.Debug|x64.ActiveCfg = Debug|Any CPU
301+
{EDF434F6-70C0-4005-B63E-0C365B3DA42A}.Debug|x64.Build.0 = Debug|Any CPU
302+
{EDF434F6-70C0-4005-B63E-0C365B3DA42A}.Debug|x86.ActiveCfg = Debug|Any CPU
303+
{EDF434F6-70C0-4005-B63E-0C365B3DA42A}.Debug|x86.Build.0 = Debug|Any CPU
304+
{EDF434F6-70C0-4005-B63E-0C365B3DA42A}.Release|Any CPU.ActiveCfg = Release|Any CPU
305+
{EDF434F6-70C0-4005-B63E-0C365B3DA42A}.Release|Any CPU.Build.0 = Release|Any CPU
306+
{EDF434F6-70C0-4005-B63E-0C365B3DA42A}.Release|ARM.ActiveCfg = Release|Any CPU
307+
{EDF434F6-70C0-4005-B63E-0C365B3DA42A}.Release|ARM.Build.0 = Release|Any CPU
308+
{EDF434F6-70C0-4005-B63E-0C365B3DA42A}.Release|x64.ActiveCfg = Release|Any CPU
309+
{EDF434F6-70C0-4005-B63E-0C365B3DA42A}.Release|x64.Build.0 = Release|Any CPU
310+
{EDF434F6-70C0-4005-B63E-0C365B3DA42A}.Release|x86.ActiveCfg = Release|Any CPU
311+
{EDF434F6-70C0-4005-B63E-0C365B3DA42A}.Release|x86.Build.0 = Release|Any CPU
294312
{E4286D1D-A98C-4516-9E47-CBF9739448DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
295313
{E4286D1D-A98C-4516-9E47-CBF9739448DA}.Debug|Any CPU.Build.0 = Debug|Any CPU
296314
{E4286D1D-A98C-4516-9E47-CBF9739448DA}.Debug|ARM.ActiveCfg = Debug|Any CPU
@@ -342,6 +360,7 @@ Global
342360
{5DC68E83-ABE0-4887-B17E-1ED4EEE89C2C} = {7971CAEB-B9F2-416B-966D-2D697C4C1E62}
343361
{437473EE-7FBB-4C28-96EC-41E1AEE161F3} = {7971CAEB-B9F2-416B-966D-2D697C4C1E62}
344362
{AD4F83E5-4240-485D-BB5C-F43974F716E4} = {7971CAEB-B9F2-416B-966D-2D697C4C1E62}
363+
{EDF434F6-70C0-4005-B63E-0C365B3DA42A} = {7971CAEB-B9F2-416B-966D-2D697C4C1E62}
345364
{E4286D1D-A98C-4516-9E47-CBF9739448DA} = {7971CAEB-B9F2-416B-966D-2D697C4C1E62}
346365
{F1880F07-238F-4A3A-9E58-141350665E1F} = {7971CAEB-B9F2-416B-966D-2D697C4C1E62}
347366
EndGlobalSection

src-console/ConsoleAppEF2.1.1/Program.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ static void Main(string[] args)
6161
};
6262
Console.WriteLine("all {0}", JsonConvert.SerializeObject(all, Formatting.Indented));
6363

64-
var config = new ParsingConfig();
64+
var config = ParsingConfig.DefaultEFCore21;
6565
config.CustomTypeProvider = new C();
6666

6767
var context = new TestContext();
@@ -79,8 +79,11 @@ static void Main(string[] args)
7979
context.SaveChanges();
8080
}
8181

82-
var carFirstOrDefault = context.Cars.Where(config, "Brand == \"Ford\"");
83-
Console.WriteLine("carFirstOrDefault {0}", JsonConvert.SerializeObject(carFirstOrDefault, Formatting.Indented));
82+
var carFirstOrDefault1 = context.Cars.Where(config, "Brand == \"Ford\"");
83+
Console.WriteLine("carFirstOrDefault1 {0}", JsonConvert.SerializeObject(carFirstOrDefault1, Formatting.Indented));
84+
85+
var carFirstOrDefault2 = context.Cars.Where(config, "Brand == @0", "Alfa");
86+
Console.WriteLine("carFirstOrDefault2 {0}", JsonConvert.SerializeObject(carFirstOrDefault2, Formatting.Indented));
8487

8588
var carsLike1 =
8689
from c in context.Cars
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.0</TargetFramework>
6+
<AssemblyName>ConsoleAppEF21</AssemblyName>
7+
<RootNamespace>ConsoleAppEF21</RootNamespace>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.1.1" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.1" />
13+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.1.1" />
14+
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<ProjectReference Include="..\..\src\System.Linq.Dynamic.Core\System.Linq.Dynamic.Core.csproj" />
19+
</ItemGroup>
20+
21+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace ConsoleAppEF21.Database
4+
{
5+
public class Car
6+
{
7+
[Key]
8+
public int Key { get; set; }
9+
10+
[Required]
11+
[StringLength(8)]
12+
public string Vin { get; set; }
13+
14+
[Required]
15+
public string Year { get; set; }
16+
17+
[Required]
18+
public string Brand { get; set; }
19+
20+
[Required]
21+
public string Color { get; set; }
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.Extensions.Logging;
3+
using Microsoft.Extensions.Logging.Console;
4+
5+
namespace ConsoleAppEF21.Database
6+
{
7+
public class TestContext : DbContext
8+
{
9+
public static readonly LoggerFactory MyLoggerFactory = new LoggerFactory(new[] { new ConsoleLoggerProvider((filter, includeScopes) => true, true) });
10+
11+
public virtual DbSet<Car> Cars { get; set; }
12+
13+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
14+
{
15+
optionsBuilder.UseLoggerFactory(MyLoggerFactory); // Warning: Do not create a new ILoggerFactory instance each time
16+
optionsBuilder.EnableSensitiveDataLogging();
17+
18+
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=CarsEF20;Trusted_Connection=True;");
19+
}
20+
21+
protected override void OnModelCreating(ModelBuilder modelBuilder)
22+
{
23+
modelBuilder.Entity<Car>().HasKey(c => c.Key);
24+
}
25+
26+
// https://stackoverflow.com/questions/46212704/how-do-i-write-ef-functions-extension-method
27+
public static bool Like(string matchExpression, string pattern) => EF.Functions.Like(matchExpression, pattern);
28+
}
29+
}
+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Linq.Dynamic.Core;
5+
using ConsoleAppEF21.Database;
6+
using Newtonsoft.Json;
7+
8+
namespace ConsoleAppEF21
9+
{
10+
class Program
11+
{
12+
//class C : AbstractDynamicLinqCustomTypeProvider, IDynamicLinkCustomTypeProvider
13+
//{
14+
// public HashSet<Type> GetCustomTypes()
15+
// {
16+
// var assemblies = AppDomain.CurrentDomain.GetAssemblies();
17+
18+
// var set = new HashSet<Type>(FindTypesMarkedWithDynamicLinqTypeAttribute(assemblies))
19+
// {
20+
// typeof(TestContext)
21+
// };
22+
23+
// return set;
24+
// }
25+
//}
26+
27+
//private static IQueryable GetQueryable()
28+
//{
29+
// var random = new Random((int)DateTime.Now.Ticks);
30+
31+
// var x = Enumerable.Range(0, 10).Select(i => new
32+
// {
33+
// Id = i,
34+
// Value = random.Next(),
35+
// });
36+
37+
// return x.AsQueryable().Select("new (it as Id, @0 as Value)", random.Next());
38+
// // return x.AsQueryable(); //x.AsQueryable().Select("new (Id, Value)");
39+
//}
40+
41+
static void Main(string[] args)
42+
{
43+
var list = new List<Car> { new Car { Key = 1 }, new Car { Key = 2 } };
44+
45+
var carsTest = list.AsQueryable().Where("Key = @0", "1").ToList();
46+
47+
//IQueryable qry = GetQueryable();
48+
49+
//var result = qry.Select("it").OrderBy("Value");
50+
//try
51+
//{
52+
// Console.WriteLine("result {0}", JsonConvert.SerializeObject(result, Formatting.Indented));
53+
//}
54+
//catch (Exception)
55+
//{
56+
// // Console.WriteLine(e);
57+
//}
58+
59+
//var all = new
60+
//{
61+
// test1 = new List<int> { 1, 2, 3 }.ToDynamicList(typeof(int)),
62+
// test2 = new List<dynamic> { 4, 5, 6 }.ToDynamicList(typeof(int)),
63+
// test3 = new List<object> { 7, 8, 9 }.ToDynamicList(typeof(int))
64+
//};
65+
// Console.WriteLine("all {0}", JsonConvert.SerializeObject(all, Formatting.None));
66+
67+
var config = ParsingConfig.DefaultEFCore21;
68+
69+
var context = new TestContext();
70+
if (!context.Cars.Any())
71+
{
72+
context.Cars.Add(new Car { Brand = "Ford", Color = "Blue", Vin = "yes", Year = "2017" });
73+
context.Cars.Add(new Car { Brand = "Fiat", Color = "Red", Vin = "yes", Year = "2016" });
74+
context.Cars.Add(new Car { Brand = "Alfa", Color = "Black", Vin = "no", Year = "1979" });
75+
context.Cars.Add(new Car { Brand = "Alfa", Color = "Black", Vin = "a%bc", Year = "1979" });
76+
context.SaveChanges();
77+
}
78+
79+
//var g1 = context.Cars.GroupBy("new(Brand)").Select("new(Key.Brand as KeyValue1, it.Count() as CountValue1)").ToDynamicList();
80+
//Console.WriteLine("GroupBy @ local {0}", JsonConvert.SerializeObject(g1, Formatting.Indented));
81+
82+
//Console.WriteLine(new string('_', 80));
83+
84+
var g2 = context.Cars.GroupBy("new(Brand)", config).Select("new(Key.Brand as KeyValue2, it.Count() as CountValue2)").ToDynamicList();
85+
Console.WriteLine("GroupBy @ database {0}", JsonConvert.SerializeObject(g2, Formatting.Indented));
86+
87+
//var carFirstOrDefault = context.Cars.Where(config, "Brand == \"Ford\"");
88+
//Console.WriteLine("carFirstOrDefault {0}", JsonConvert.SerializeObject(carFirstOrDefault, Formatting.Indented));
89+
90+
//var carsLike1 =
91+
// from c in context.Cars
92+
// where EF.Functions.Like(c.Brand, "%a%")
93+
// select c;
94+
//Console.WriteLine("carsLike1 {0}", JsonConvert.SerializeObject(carsLike1, Formatting.Indented));
95+
96+
//var cars2Like = context.Cars.Where(c => EF.Functions.Like(c.Brand, "%a%"));
97+
//Console.WriteLine("cars2Like {0}", JsonConvert.SerializeObject(cars2Like, Formatting.Indented));
98+
99+
//var dynamicCarsLike1 = context.Cars.Where(config, "TestContext.Like(Brand, \"%a%\")");
100+
//Console.WriteLine("dynamicCarsLike1 {0}", JsonConvert.SerializeObject(dynamicCarsLike1, Formatting.Indented));
101+
102+
//var dynamicCarsLike2 = context.Cars.Where(config, "TestContext.Like(Brand, \"%d%\")");
103+
//Console.WriteLine("dynamicCarsLike2 {0}", JsonConvert.SerializeObject(dynamicCarsLike2, Formatting.Indented));
104+
105+
//var dynamicFunctionsLike1 = context.Cars.Where(config, "DynamicFunctions.Like(Brand, \"%a%\")");
106+
//Console.WriteLine("dynamicFunctionsLike1 {0}", JsonConvert.SerializeObject(dynamicFunctionsLike1, Formatting.Indented));
107+
108+
//var dynamicFunctionsLike2 = context.Cars.Where(config, "DynamicFunctions.Like(Vin, \"%a.%b%\", \".\")");
109+
//Console.WriteLine("dynamicFunctionsLike2 {0}", JsonConvert.SerializeObject(dynamicFunctionsLike2, Formatting.Indented));
110+
111+
//var testDynamic = context.Cars.Select(c => new
112+
//{
113+
// K = c.Key,
114+
// C = c.Color
115+
//});
116+
117+
//var testDynamicResult = testDynamic.Select("it").OrderBy("C");
118+
//try
119+
//{
120+
// Console.WriteLine("resultX {0}", JsonConvert.SerializeObject(testDynamicResult, Formatting.Indented));
121+
//}
122+
//catch (Exception e)
123+
//{
124+
// Console.WriteLine(e);
125+
//}
126+
}
127+
}
128+
}

src/EntityFramework.DynamicLinq/EntityFramework.DynamicLinq.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<SignAssembly>true</SignAssembly>
1313
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
1414
<PackageTags>system;linq;dynamic;entityframework;core;async</PackageTags>
15-
<PackageReleaseNotes>Bugfixes and new Features. For details see the release notes.</PackageReleaseNotes>
15+
<PackageReleaseNotes>Bugfixes and new Features. For details see CHANGELOG.md</PackageReleaseNotes>
1616
<PackageProjectUrl>https://github.com/StefH/System.Linq.Dynamic.Core</PackageProjectUrl>
1717
<PackageLicenseUrl>https://github.com/StefH/System.Linq.Dynamic.Core/blob/master/licence.txt</PackageLicenseUrl>
1818
<RepositoryType>git</RepositoryType>
@@ -57,4 +57,4 @@
5757
<Reference Include="System" />
5858
<Reference Include="Microsoft.CSharp" />
5959
</ItemGroup>
60-
</Project>
60+
</Project>

src/Microsoft.EntityFrameworkCore.DynamicLinq/Microsoft.EntityFrameworkCore.DynamicLinq.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<SignAssembly>true</SignAssembly>
1313
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
1414
<PackageTags>system;linq;dynamic;entityframework;core;async</PackageTags>
15-
<PackageReleaseNotes>Bugfixes and new Features. For details see the release notes.</PackageReleaseNotes>
15+
<PackageReleaseNotes>Bugfixes and new Features. For details see CHANGELOG.md</PackageReleaseNotes>
1616
<PackageProjectUrl>https://github.com/StefH/System.Linq.Dynamic.Core</PackageProjectUrl>
1717
<PackageLicenseUrl>https://github.com/StefH/System.Linq.Dynamic.Core/blob/master/licence.txt</PackageLicenseUrl>
1818
<RepositoryType>git</RepositoryType>

0 commit comments

Comments
 (0)