Skip to content

Commit

Permalink
Added EvaluateGroupByAtDatabase For EF Core 2.1 (#197)
Browse files Browse the repository at this point in the history
* EvaluateGroupByAtDatabase (#165)

* wip

* Added ParsingConfig.DefaultEFCore21

* Added EvaluateGroupByAtDatabase to Select and SelectMany
  • Loading branch information
StefH authored Aug 17, 2018
1 parent c730a78 commit 1e0ed4e
Show file tree
Hide file tree
Showing 12 changed files with 259 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ The following frameworks are supported:
- net45 and up
- netstandard1.3
- netstandard2.0
- uap10.0
- uap10.0 (TODO...)s

## Fork details
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.
Expand Down
19 changes: 19 additions & 0 deletions System.Linq.Dynamic.Core.sln
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp_netcore2.0_EF2.0
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MemoryLeakTest167", "src-console\MemoryLeakTest167\MemoryLeakTest167.csproj", "{AD4F83E5-4240-485D-BB5C-F43974F716E4}"
EndProject
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}"
EndProject
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}"
EndProject
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}"
Expand Down Expand Up @@ -291,6 +293,22 @@ Global
{AD4F83E5-4240-485D-BB5C-F43974F716E4}.Release|x64.Build.0 = Release|Any CPU
{AD4F83E5-4240-485D-BB5C-F43974F716E4}.Release|x86.ActiveCfg = Release|Any CPU
{AD4F83E5-4240-485D-BB5C-F43974F716E4}.Release|x86.Build.0 = Release|Any CPU
{EDF434F6-70C0-4005-B63E-0C365B3DA42A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EDF434F6-70C0-4005-B63E-0C365B3DA42A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EDF434F6-70C0-4005-B63E-0C365B3DA42A}.Debug|ARM.ActiveCfg = Debug|Any CPU
{EDF434F6-70C0-4005-B63E-0C365B3DA42A}.Debug|ARM.Build.0 = Debug|Any CPU
{EDF434F6-70C0-4005-B63E-0C365B3DA42A}.Debug|x64.ActiveCfg = Debug|Any CPU
{EDF434F6-70C0-4005-B63E-0C365B3DA42A}.Debug|x64.Build.0 = Debug|Any CPU
{EDF434F6-70C0-4005-B63E-0C365B3DA42A}.Debug|x86.ActiveCfg = Debug|Any CPU
{EDF434F6-70C0-4005-B63E-0C365B3DA42A}.Debug|x86.Build.0 = Debug|Any CPU
{EDF434F6-70C0-4005-B63E-0C365B3DA42A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EDF434F6-70C0-4005-B63E-0C365B3DA42A}.Release|Any CPU.Build.0 = Release|Any CPU
{EDF434F6-70C0-4005-B63E-0C365B3DA42A}.Release|ARM.ActiveCfg = Release|Any CPU
{EDF434F6-70C0-4005-B63E-0C365B3DA42A}.Release|ARM.Build.0 = Release|Any CPU
{EDF434F6-70C0-4005-B63E-0C365B3DA42A}.Release|x64.ActiveCfg = Release|Any CPU
{EDF434F6-70C0-4005-B63E-0C365B3DA42A}.Release|x64.Build.0 = Release|Any CPU
{EDF434F6-70C0-4005-B63E-0C365B3DA42A}.Release|x86.ActiveCfg = Release|Any CPU
{EDF434F6-70C0-4005-B63E-0C365B3DA42A}.Release|x86.Build.0 = Release|Any CPU
{E4286D1D-A98C-4516-9E47-CBF9739448DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E4286D1D-A98C-4516-9E47-CBF9739448DA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E4286D1D-A98C-4516-9E47-CBF9739448DA}.Debug|ARM.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -342,6 +360,7 @@ Global
{5DC68E83-ABE0-4887-B17E-1ED4EEE89C2C} = {7971CAEB-B9F2-416B-966D-2D697C4C1E62}
{437473EE-7FBB-4C28-96EC-41E1AEE161F3} = {7971CAEB-B9F2-416B-966D-2D697C4C1E62}
{AD4F83E5-4240-485D-BB5C-F43974F716E4} = {7971CAEB-B9F2-416B-966D-2D697C4C1E62}
{EDF434F6-70C0-4005-B63E-0C365B3DA42A} = {7971CAEB-B9F2-416B-966D-2D697C4C1E62}
{E4286D1D-A98C-4516-9E47-CBF9739448DA} = {7971CAEB-B9F2-416B-966D-2D697C4C1E62}
{F1880F07-238F-4A3A-9E58-141350665E1F} = {7971CAEB-B9F2-416B-966D-2D697C4C1E62}
EndGlobalSection
Expand Down
9 changes: 6 additions & 3 deletions src-console/ConsoleAppEF2.1.1/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static void Main(string[] args)
};
Console.WriteLine("all {0}", JsonConvert.SerializeObject(all, Formatting.Indented));

var config = new ParsingConfig();
var config = ParsingConfig.DefaultEFCore21;
config.CustomTypeProvider = new C();

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

var carFirstOrDefault = context.Cars.Where(config, "Brand == \"Ford\"");
Console.WriteLine("carFirstOrDefault {0}", JsonConvert.SerializeObject(carFirstOrDefault, Formatting.Indented));
var carFirstOrDefault1 = context.Cars.Where(config, "Brand == \"Ford\"");
Console.WriteLine("carFirstOrDefault1 {0}", JsonConvert.SerializeObject(carFirstOrDefault1, Formatting.Indented));

var carFirstOrDefault2 = context.Cars.Where(config, "Brand == @0", "Alfa");
Console.WriteLine("carFirstOrDefault2 {0}", JsonConvert.SerializeObject(carFirstOrDefault2, Formatting.Indented));

var carsLike1 =
from c in context.Cars
Expand Down
21 changes: 21 additions & 0 deletions src-console/ConsoleAppEF2.1/ConsoleApp_netcore2.0_EF2.1.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<AssemblyName>ConsoleAppEF21</AssemblyName>
<RootNamespace>ConsoleAppEF21</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\System.Linq.Dynamic.Core\System.Linq.Dynamic.Core.csproj" />
</ItemGroup>

</Project>
23 changes: 23 additions & 0 deletions src-console/ConsoleAppEF2.1/Database/Car.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.ComponentModel.DataAnnotations;

namespace ConsoleAppEF21.Database
{
public class Car
{
[Key]
public int Key { get; set; }

[Required]
[StringLength(8)]
public string Vin { get; set; }

[Required]
public string Year { get; set; }

[Required]
public string Brand { get; set; }

[Required]
public string Color { get; set; }
}
}
29 changes: 29 additions & 0 deletions src-console/ConsoleAppEF2.1/Database/TestContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;

namespace ConsoleAppEF21.Database
{
public class TestContext : DbContext
{
public static readonly LoggerFactory MyLoggerFactory = new LoggerFactory(new[] { new ConsoleLoggerProvider((filter, includeScopes) => true, true) });

public virtual DbSet<Car> Cars { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseLoggerFactory(MyLoggerFactory); // Warning: Do not create a new ILoggerFactory instance each time
optionsBuilder.EnableSensitiveDataLogging();

optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=CarsEF20;Trusted_Connection=True;");
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Car>().HasKey(c => c.Key);
}

// https://stackoverflow.com/questions/46212704/how-do-i-write-ef-functions-extension-method
public static bool Like(string matchExpression, string pattern) => EF.Functions.Like(matchExpression, pattern);
}
}
128 changes: 128 additions & 0 deletions src-console/ConsoleAppEF2.1/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using ConsoleAppEF21.Database;
using Newtonsoft.Json;

namespace ConsoleAppEF21
{
class Program
{
//class C : AbstractDynamicLinqCustomTypeProvider, IDynamicLinkCustomTypeProvider
//{
// public HashSet<Type> GetCustomTypes()
// {
// var assemblies = AppDomain.CurrentDomain.GetAssemblies();

// var set = new HashSet<Type>(FindTypesMarkedWithDynamicLinqTypeAttribute(assemblies))
// {
// typeof(TestContext)
// };

// return set;
// }
//}

//private static IQueryable GetQueryable()
//{
// var random = new Random((int)DateTime.Now.Ticks);

// var x = Enumerable.Range(0, 10).Select(i => new
// {
// Id = i,
// Value = random.Next(),
// });

// return x.AsQueryable().Select("new (it as Id, @0 as Value)", random.Next());
// // return x.AsQueryable(); //x.AsQueryable().Select("new (Id, Value)");
//}

static void Main(string[] args)
{
var list = new List<Car> { new Car { Key = 1 }, new Car { Key = 2 } };

var carsTest = list.AsQueryable().Where("Key = @0", "1").ToList();

//IQueryable qry = GetQueryable();

//var result = qry.Select("it").OrderBy("Value");
//try
//{
// Console.WriteLine("result {0}", JsonConvert.SerializeObject(result, Formatting.Indented));
//}
//catch (Exception)
//{
// // Console.WriteLine(e);
//}

//var all = new
//{
// test1 = new List<int> { 1, 2, 3 }.ToDynamicList(typeof(int)),
// test2 = new List<dynamic> { 4, 5, 6 }.ToDynamicList(typeof(int)),
// test3 = new List<object> { 7, 8, 9 }.ToDynamicList(typeof(int))
//};
// Console.WriteLine("all {0}", JsonConvert.SerializeObject(all, Formatting.None));

var config = ParsingConfig.DefaultEFCore21;

var context = new TestContext();
if (!context.Cars.Any())
{
context.Cars.Add(new Car { Brand = "Ford", Color = "Blue", Vin = "yes", Year = "2017" });
context.Cars.Add(new Car { Brand = "Fiat", Color = "Red", Vin = "yes", Year = "2016" });
context.Cars.Add(new Car { Brand = "Alfa", Color = "Black", Vin = "no", Year = "1979" });
context.Cars.Add(new Car { Brand = "Alfa", Color = "Black", Vin = "a%bc", Year = "1979" });
context.SaveChanges();
}

//var g1 = context.Cars.GroupBy("new(Brand)").Select("new(Key.Brand as KeyValue1, it.Count() as CountValue1)").ToDynamicList();
//Console.WriteLine("GroupBy @ local {0}", JsonConvert.SerializeObject(g1, Formatting.Indented));

//Console.WriteLine(new string('_', 80));

var g2 = context.Cars.GroupBy("new(Brand)", config).Select("new(Key.Brand as KeyValue2, it.Count() as CountValue2)").ToDynamicList();
Console.WriteLine("GroupBy @ database {0}", JsonConvert.SerializeObject(g2, Formatting.Indented));

//var carFirstOrDefault = context.Cars.Where(config, "Brand == \"Ford\"");
//Console.WriteLine("carFirstOrDefault {0}", JsonConvert.SerializeObject(carFirstOrDefault, Formatting.Indented));

//var carsLike1 =
// from c in context.Cars
// where EF.Functions.Like(c.Brand, "%a%")
// select c;
//Console.WriteLine("carsLike1 {0}", JsonConvert.SerializeObject(carsLike1, Formatting.Indented));

//var cars2Like = context.Cars.Where(c => EF.Functions.Like(c.Brand, "%a%"));
//Console.WriteLine("cars2Like {0}", JsonConvert.SerializeObject(cars2Like, Formatting.Indented));

//var dynamicCarsLike1 = context.Cars.Where(config, "TestContext.Like(Brand, \"%a%\")");
//Console.WriteLine("dynamicCarsLike1 {0}", JsonConvert.SerializeObject(dynamicCarsLike1, Formatting.Indented));

//var dynamicCarsLike2 = context.Cars.Where(config, "TestContext.Like(Brand, \"%d%\")");
//Console.WriteLine("dynamicCarsLike2 {0}", JsonConvert.SerializeObject(dynamicCarsLike2, Formatting.Indented));

//var dynamicFunctionsLike1 = context.Cars.Where(config, "DynamicFunctions.Like(Brand, \"%a%\")");
//Console.WriteLine("dynamicFunctionsLike1 {0}", JsonConvert.SerializeObject(dynamicFunctionsLike1, Formatting.Indented));

//var dynamicFunctionsLike2 = context.Cars.Where(config, "DynamicFunctions.Like(Vin, \"%a.%b%\", \".\")");
//Console.WriteLine("dynamicFunctionsLike2 {0}", JsonConvert.SerializeObject(dynamicFunctionsLike2, Formatting.Indented));

//var testDynamic = context.Cars.Select(c => new
//{
// K = c.Key,
// C = c.Color
//});

//var testDynamicResult = testDynamic.Select("it").OrderBy("C");
//try
//{
// Console.WriteLine("resultX {0}", JsonConvert.SerializeObject(testDynamicResult, Formatting.Indented));
//}
//catch (Exception e)
//{
// Console.WriteLine(e);
//}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<SignAssembly>true</SignAssembly>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
<PackageTags>system;linq;dynamic;entityframework;core;async</PackageTags>
<PackageReleaseNotes>Bugfixes and new Features. For details see the release notes.</PackageReleaseNotes>
<PackageReleaseNotes>Bugfixes and new Features. For details see CHANGELOG.md</PackageReleaseNotes>
<PackageProjectUrl>https://github.com/StefH/System.Linq.Dynamic.Core</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/StefH/System.Linq.Dynamic.Core/blob/master/licence.txt</PackageLicenseUrl>
<RepositoryType>git</RepositoryType>
Expand Down Expand Up @@ -57,4 +57,4 @@
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<SignAssembly>true</SignAssembly>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
<PackageTags>system;linq;dynamic;entityframework;core;async</PackageTags>
<PackageReleaseNotes>Bugfixes and new Features. For details see the release notes.</PackageReleaseNotes>
<PackageReleaseNotes>Bugfixes and new Features. For details see CHANGELOG.md</PackageReleaseNotes>
<PackageProjectUrl>https://github.com/StefH/System.Linq.Dynamic.Core</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/StefH/System.Linq.Dynamic.Core/blob/master/licence.txt</PackageLicenseUrl>
<RepositoryType>git</RepositoryType>
Expand Down
Loading

0 comments on commit 1e0ed4e

Please sign in to comment.