Skip to content

Commit f48b512

Browse files
authored
Performance fix (#153) (#159)
* Performance fix * ConsoleApp_netcore2.0_EF2.0.2_InMemory
1 parent 4676b40 commit f48b512

12 files changed

+376
-112
lines changed

System.Linq.Dynamic.Core.sln

+20-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test-console", "test-consol
3939
EndProject
4040
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test-uap", "test-uap", "{ECA5702B-5D32-4888-A34E-9461FC533F23}"
4141
EndProject
42-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp_netcore2.0_EF2.0.1", "src-console\ConsoleAppEF2.0\ConsoleApp_netcore2.0_EF2.0.1.csproj", "{60CE11E0-E057-45A2-8F8A-73B1BD045BFB}"
42+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp_netcore2.0_EF2.0.1", "src-console\ConsoleAppEF2.0\ConsoleApp_netcore2.0_EF2.0.1.csproj", "{60CE11E0-E057-45A2-8F8A-73B1BD045BFB}"
4343
EndProject
4444
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp_netcore1.1_EF1.1.2", "src-console\ConsoleAppEF1.1\ConsoleApp_netcore1.1_EF1.1.2.csproj", "{59E67CE3-5DB7-4D37-B308-E4E22E3DE4F4}"
4545
EndProject
@@ -49,6 +49,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp_net40_sqlite", "
4949
EndProject
5050
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp_net452_EF6", "src-console\ConsoleApp_net452_EF6\ConsoleApp_net452_EF6.csproj", "{5DC68E83-ABE0-4887-B17E-1ED4EEE89C2C}"
5151
EndProject
52+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp_netcore2.0_EF2.0.2_InMemory", "src-console\ConsoleAppEF2.0.2_InMemory\ConsoleApp_netcore2.0_EF2.0.2_InMemory.csproj", "{437473EE-7FBB-4C28-96EC-41E1AEE161F3}"
53+
EndProject
5254
Global
5355
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5456
Debug|Any CPU = Debug|Any CPU
@@ -251,6 +253,22 @@ Global
251253
{5DC68E83-ABE0-4887-B17E-1ED4EEE89C2C}.Release|x64.Build.0 = Release|Any CPU
252254
{5DC68E83-ABE0-4887-B17E-1ED4EEE89C2C}.Release|x86.ActiveCfg = Release|Any CPU
253255
{5DC68E83-ABE0-4887-B17E-1ED4EEE89C2C}.Release|x86.Build.0 = Release|Any CPU
256+
{437473EE-7FBB-4C28-96EC-41E1AEE161F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
257+
{437473EE-7FBB-4C28-96EC-41E1AEE161F3}.Debug|Any CPU.Build.0 = Debug|Any CPU
258+
{437473EE-7FBB-4C28-96EC-41E1AEE161F3}.Debug|ARM.ActiveCfg = Debug|Any CPU
259+
{437473EE-7FBB-4C28-96EC-41E1AEE161F3}.Debug|ARM.Build.0 = Debug|Any CPU
260+
{437473EE-7FBB-4C28-96EC-41E1AEE161F3}.Debug|x64.ActiveCfg = Debug|Any CPU
261+
{437473EE-7FBB-4C28-96EC-41E1AEE161F3}.Debug|x64.Build.0 = Debug|Any CPU
262+
{437473EE-7FBB-4C28-96EC-41E1AEE161F3}.Debug|x86.ActiveCfg = Debug|Any CPU
263+
{437473EE-7FBB-4C28-96EC-41E1AEE161F3}.Debug|x86.Build.0 = Debug|Any CPU
264+
{437473EE-7FBB-4C28-96EC-41E1AEE161F3}.Release|Any CPU.ActiveCfg = Release|Any CPU
265+
{437473EE-7FBB-4C28-96EC-41E1AEE161F3}.Release|Any CPU.Build.0 = Release|Any CPU
266+
{437473EE-7FBB-4C28-96EC-41E1AEE161F3}.Release|ARM.ActiveCfg = Release|Any CPU
267+
{437473EE-7FBB-4C28-96EC-41E1AEE161F3}.Release|ARM.Build.0 = Release|Any CPU
268+
{437473EE-7FBB-4C28-96EC-41E1AEE161F3}.Release|x64.ActiveCfg = Release|Any CPU
269+
{437473EE-7FBB-4C28-96EC-41E1AEE161F3}.Release|x64.Build.0 = Release|Any CPU
270+
{437473EE-7FBB-4C28-96EC-41E1AEE161F3}.Release|x86.ActiveCfg = Release|Any CPU
271+
{437473EE-7FBB-4C28-96EC-41E1AEE161F3}.Release|x86.Build.0 = Release|Any CPU
254272
EndGlobalSection
255273
GlobalSection(SolutionProperties) = preSolution
256274
HideSolutionNode = FALSE
@@ -268,6 +286,7 @@ Global
268286
{0EB12661-F5CF-4071-9812-F4C8AF3D4531} = {7971CAEB-B9F2-416B-966D-2D697C4C1E62}
269287
{E587974C-19A9-426A-A952-216D5F883EB6} = {7971CAEB-B9F2-416B-966D-2D697C4C1E62}
270288
{5DC68E83-ABE0-4887-B17E-1ED4EEE89C2C} = {7971CAEB-B9F2-416B-966D-2D697C4C1E62}
289+
{437473EE-7FBB-4C28-96EC-41E1AEE161F3} = {7971CAEB-B9F2-416B-966D-2D697C4C1E62}
271290
EndGlobalSection
272291
GlobalSection(ExtensibilityGlobals) = postSolution
273292
SolutionGuid = {94C56722-194E-4B8B-BC23-B3F754E89A20}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.0</TargetFramework>
6+
<AssemblyName>ConsoleAppEF2</AssemblyName>
7+
<RootNamespace>ConsoleAppEF2</RootNamespace>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.0.2" />
12+
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<ProjectReference Include="..\..\src\Microsoft.EntityFrameworkCore.DynamicLinq\Microsoft.EntityFrameworkCore.DynamicLinq.csproj" />
17+
</ItemGroup>
18+
19+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace ConsoleAppEF2.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,22 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
namespace ConsoleAppEF2.Database
4+
{
5+
public class TestContext : DbContext
6+
{
7+
public virtual DbSet<Car> Cars { get; set; }
8+
9+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
10+
{
11+
optionsBuilder.UseInMemoryDatabase("cars");
12+
}
13+
14+
protected override void OnModelCreating(ModelBuilder modelBuilder)
15+
{
16+
modelBuilder.Entity<Car>().HasKey(c => c.Key);
17+
}
18+
19+
// https://stackoverflow.com/questions/46212704/how-do-i-write-ef-functions-extension-method
20+
public static bool Like(string matchExpression, string pattern) => EF.Functions.Like(matchExpression, pattern);
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Dynamic;
4+
using System.Linq;
5+
using System.Linq.Dynamic.Core;
6+
using System.Linq.Dynamic.Core.CustomTypeProviders;
7+
using ConsoleAppEF2.Database;
8+
using Microsoft.EntityFrameworkCore;
9+
using Newtonsoft.Json;
10+
11+
namespace ConsoleAppEF2
12+
{
13+
class Program
14+
{
15+
class C : AbstractDynamicLinqCustomTypeProvider, IDynamicLinkCustomTypeProvider
16+
{
17+
public HashSet<Type> GetCustomTypes()
18+
{
19+
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
20+
21+
var set = new HashSet<Type>(FindTypesMarkedWithDynamicLinqTypeAttribute(assemblies))
22+
{
23+
typeof(TestContext)
24+
};
25+
26+
return set;
27+
}
28+
}
29+
30+
private static IQueryable GetQueryable()
31+
{
32+
var random = new Random((int)DateTime.Now.Ticks);
33+
34+
var x = Enumerable.Range(0, 10).Select(i => new
35+
{
36+
Id = i,
37+
Value = random.Next(),
38+
});
39+
40+
return x.AsQueryable().Select("new (it as Id, @0 as Value)", random.Next());
41+
// return x.AsQueryable(); //x.AsQueryable().Select("new (Id, Value)");
42+
}
43+
44+
static void Main(string[] args)
45+
{
46+
IQueryable qry = GetQueryable();
47+
48+
var result = qry.Select("it").OrderBy("Value");
49+
try
50+
{
51+
Console.WriteLine("result {0}", JsonConvert.SerializeObject(result, Formatting.Indented));
52+
}
53+
catch (Exception)
54+
{
55+
// Console.WriteLine(e);
56+
}
57+
58+
var all = new
59+
{
60+
test1 = new List<int> { 1, 2, 3 }.ToDynamicList(typeof(int)),
61+
test2 = new List<dynamic> { 4, 5, 6 }.ToDynamicList(typeof(int)),
62+
test3 = new List<object> { 7, 8, 9 }.ToDynamicList(typeof(int))
63+
};
64+
Console.WriteLine("all {0}", JsonConvert.SerializeObject(all, Formatting.Indented));
65+
66+
var config = new ParsingConfig
67+
{
68+
CustomTypeProvider = new C()
69+
};
70+
71+
var context = new TestContext();
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+
var carFirstOrDefault = context.Cars.Where(config, "Brand == \"Ford\"");
79+
Console.WriteLine("carFirstOrDefault {0}", JsonConvert.SerializeObject(carFirstOrDefault, Formatting.Indented));
80+
81+
var carsLike1 =
82+
from c in context.Cars
83+
where EF.Functions.Like(c.Brand, "%a%")
84+
select c;
85+
Console.WriteLine("carsLike1 {0}", JsonConvert.SerializeObject(carsLike1, Formatting.Indented));
86+
87+
var cars2Like = context.Cars.Where(c => EF.Functions.Like(c.Brand, "%a%"));
88+
Console.WriteLine("cars2Like {0}", JsonConvert.SerializeObject(cars2Like, Formatting.Indented));
89+
90+
var dynamicCarsLike1 = context.Cars.Where(config, "TestContext.Like(Brand, \"%a%\")");
91+
Console.WriteLine("dynamicCarsLike1 {0}", JsonConvert.SerializeObject(dynamicCarsLike1, Formatting.Indented));
92+
93+
var dynamicCarsLike2 = context.Cars.Where(config, "TestContext.Like(Brand, \"%d%\")");
94+
Console.WriteLine("dynamicCarsLike2 {0}", JsonConvert.SerializeObject(dynamicCarsLike2, Formatting.Indented));
95+
96+
var dynamicFunctionsLike1 = context.Cars.Where(config, "DynamicFunctions.Like(Brand, \"%a%\")");
97+
Console.WriteLine("dynamicFunctionsLike1 {0}", JsonConvert.SerializeObject(dynamicFunctionsLike1, Formatting.Indented));
98+
99+
var dynamicFunctionsLike2 = context.Cars.Where(config, "DynamicFunctions.Like(Vin, \"%a.%b%\", \".\")");
100+
Console.WriteLine("dynamicFunctionsLike2 {0}", JsonConvert.SerializeObject(dynamicFunctionsLike2, Formatting.Indented));
101+
102+
var testDynamic = context.Cars.Select(c => new
103+
{
104+
K = c.Key,
105+
C = c.Color
106+
});
107+
108+
var testDynamicResult = testDynamic.Select("it").OrderBy("C");
109+
try
110+
{
111+
Console.WriteLine("resultX {0}", JsonConvert.SerializeObject(testDynamicResult, Formatting.Indented));
112+
}
113+
catch (Exception e)
114+
{
115+
Console.WriteLine(e);
116+
}
117+
}
118+
}
119+
}

src-console/ConsoleAppEF2.0/Program.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ static void Main(string[] args)
4949
{
5050
Console.WriteLine("result {0}", JsonConvert.SerializeObject(result, Formatting.Indented));
5151
}
52-
catch (Exception e)
52+
catch (Exception)
5353
{
54-
Console.WriteLine(e);
54+
// Console.WriteLine(e);
5555
}
5656

5757
var all = new

0 commit comments

Comments
 (0)