Skip to content

Commit 6fc7fcc

Browse files
authored
Add support for OfType, Is, As and Cast (#249)
* OfType and Cast * cast and offtype * 1.0.11 * Add NetCoreApp target & include DefaultDynamicLinqCustomTypeProvider (#251) * netcoreapp2.1 * update program.cs * OfType Function (#253) * wip * Comment unused code * Function AsType, Is, As and Cast * fix * fix * Resolve types by simple name #252 (#254) * SimpleName * add tests * solve code issue * fix code complexity * e != null * TypeFinder * . * add test * update tests * remove it.OfType(...)
1 parent 62c691c commit 6fc7fcc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1019
-287
lines changed

Directory.Build.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
</PropertyGroup>
55

66
<PropertyGroup>
7-
<VersionPrefix>1.0.10</VersionPrefix>
7+
<VersionPrefix>1.0.11</VersionPrefix>
88
</PropertyGroup>
99

1010
<Choose>

src-console/ConsoleAppEF2.0.2_InMemory/ConsoleApp_netcore2.0_EF2.0.2_InMemory.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<Compile Include="..\ConsoleAppEF2.0\Database\BaseDto.cs" Link="Database\BaseDto.cs" />
1414
<Compile Include="..\ConsoleAppEF2.0\Database\TestDto.cs" Link="Database\TestDto.cs" />
1515
<Compile Include="..\ConsoleAppEF2.0\Database\OtherTestDto.cs" Link="Database\OtherTestDto.cs" />
16+
<Compile Include="..\ConsoleAppEF2.0\Database\ComplexDto.cs" Link="Database\ComplexDto.cs" />
1617
</ItemGroup>
1718

1819
<ItemGroup>

src-console/ConsoleAppEF2.0.2_InMemory/Database/TestContext.cs

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public class TestContext : DbContext
1414

1515
public virtual DbSet<BaseDto> BaseDtos { get; set; }
1616

17+
public virtual DbSet<ComplexDto> ComplexDtos { get; set; }
18+
1719
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
1820
{
1921
optionsBuilder.UseLoggerFactory(MyLoggerFactory); // Warning: Do not create a new ILoggerFactory instance each time
@@ -27,6 +29,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
2729
modelBuilder.Entity<Car>().HasKey(c => c.Key);
2830
modelBuilder.Entity<Brand>().HasKey(b => b.BrandType);
2931
modelBuilder.Entity<BaseDto>().HasKey(t => t.Key);
32+
modelBuilder.Entity<ComplexDto>().HasKey(t => t.Key);
3033
}
3134

3235
// https://stackoverflow.com/questions/46212704/how-do-i-write-ef-functions-extension-method

src-console/ConsoleAppEF2.0.2_InMemory/Program.cs

+6
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ public Type ResolveType(string typeName)
4141

4242
return ResolveType(assemblies, typeName);
4343
}
44+
45+
public Type ResolveTypeBySimpleName(string typeName)
46+
{
47+
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
48+
return ResolveTypeBySimpleName(assemblies, typeName);
49+
}
4450
}
4551

4652
private static IQueryable GetQueryable()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Collections.Generic;
2+
using System.ComponentModel.DataAnnotations;
3+
4+
namespace ConsoleAppEF2.Database
5+
{
6+
public class ComplexDto
7+
{
8+
[Key]
9+
public int Key { get; set; }
10+
11+
public string X { get; set; }
12+
13+
public IEnumerable<BaseDto> ListOfBaseDtos { get; set; }
14+
}
15+
}

src-console/ConsoleAppEF2.0/Program.cs

+6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ public Type ResolveType(string typeName)
3030
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
3131
return ResolveType(assemblies, typeName);
3232
}
33+
34+
public Type ResolveTypeBySimpleName(string typeName)
35+
{
36+
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
37+
return ResolveTypeBySimpleName(assemblies, typeName);
38+
}
3339
}
3440

3541
private static IQueryable GetQueryable()

src-console/ConsoleAppEF2.1.1/Program.cs

+6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ public Type ResolveType(string typeName)
3131

3232
return ResolveType(assemblies, typeName);
3333
}
34+
35+
public Type ResolveTypeBySimpleName(string typeName)
36+
{
37+
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
38+
return ResolveTypeBySimpleName(assemblies, typeName);
39+
}
3440
}
3541

3642
private static IQueryable GetQueryable()

src-console/ConsoleAppEF2.1.1_InMemory/ConsoleApp_netcore2.1_EF2.1.1_InMemory.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<Compile Include="..\ConsoleAppEF2.0\Database\BaseDto.cs" Link="Database\BaseDto.cs" />
1515
<Compile Include="..\ConsoleAppEF2.0\Database\TestDto.cs" Link="Database\TestDto.cs" />
1616
<Compile Include="..\ConsoleAppEF2.0\Database\OtherTestDto.cs" Link="Database\OtherTestDto.cs" />
17+
<Compile Include="..\ConsoleAppEF2.0\Database\ComplexDto.cs" Link="Database\ComplexDto.cs" />
1718
</ItemGroup>
1819

1920
<ItemGroup>

src-console/ConsoleAppEF2.1.1_InMemory/Program.cs

+125-71
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public class NestedDto3
3232
public int Id { get; set; }
3333
}
3434

35-
class NetCore21CustomTypeProvider : AbstractDynamicLinqCustomTypeProvider, IDynamicLinkCustomTypeProvider
35+
class TestCustomTypeProvider : DefaultDynamicLinqCustomTypeProvider, IDynamicLinkCustomTypeProvider
3636
{
37-
public HashSet<Type> GetCustomTypes()
37+
public new HashSet<Type> GetCustomTypes()
3838
{
3939
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
4040

@@ -45,68 +45,60 @@ public HashSet<Type> GetCustomTypes()
4545

4646
return set;
4747
}
48-
49-
public Type ResolveType(string typeName)
50-
{
51-
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
52-
return ResolveType(assemblies, typeName);
53-
}
5448
}
5549

5650
static void Main(string[] args)
5751
{
58-
var q = new[] { new NestedDto(), new NestedDto { NestedDto2 = new NestedDto2 { NestedDto3 = new NestedDto3 { Id = 42 } } } }.AsQueryable();
52+
//var q = new[] { new NestedDto(), new NestedDto { NestedDto2 = new NestedDto2 { NestedDto3 = new NestedDto3 { Id = 42 } } } }.AsQueryable();
5953

60-
var np1 = q.Select("np(it.NestedDto2.NestedDto3.Id, 0)");
61-
var npResult1 = np1.ToDynamicList<int>();
62-
Console.WriteLine("npResult1 {0}", JsonConvert.SerializeObject(npResult1, Formatting.Indented));
54+
//var np1 = q.Select("np(it.NestedDto2.NestedDto3.Id, 0)");
55+
//var npResult1 = np1.ToDynamicList<int>();
56+
//Console.WriteLine("npResult1 {0}", JsonConvert.SerializeObject(npResult1, Formatting.Indented));
6357

64-
var np2 = q.Select("np(it.NestedDto2.NestedDto3.Id)");
65-
var npResult2 = np2.ToDynamicList<int?>();
66-
Console.WriteLine("npResult2 {0}", JsonConvert.SerializeObject(npResult2, Formatting.Indented));
58+
//var np2 = q.Select("np(it.NestedDto2.NestedDto3.Id)");
59+
//var npResult2 = np2.ToDynamicList<int?>();
60+
//Console.WriteLine("npResult2 {0}", JsonConvert.SerializeObject(npResult2, Formatting.Indented));
6761

68-
var r1 = q.Select("it != null && it.NestedDto2 != null ? it.NestedDto2.Id : null");
69-
var list1 = r1.ToDynamicList<int?>();
62+
//var r1 = q.Select("it != null && it.NestedDto2 != null ? it.NestedDto2.Id : null");
63+
//var list1 = r1.ToDynamicList<int?>();
7064

71-
var r2 = q.Select("it != null && it.NestedDto2 != null ? it.NestedDto2 : null");
72-
var list2 = r2.ToDynamicList<NestedDto2>();
65+
//var r2 = q.Select("it != null && it.NestedDto2 != null ? it.NestedDto2 : null");
66+
//var list2 = r2.ToDynamicList<NestedDto2>();
7367

7468
var config = new ParsingConfig
7569
{
7670
AllowNewToEvaluateAnyType = true,
77-
CustomTypeProvider = new NetCore21CustomTypeProvider()
78-
};
79-
80-
// Act
81-
var testDataAsQueryable = new List<string> { "name1", "name2" }.AsQueryable();
82-
var projectedData = (IQueryable<NestedDto>)testDataAsQueryable.Select(config, $"new {typeof(NestedDto).FullName}(~ as Name)");
83-
Console.WriteLine(projectedData.First().Name);
84-
Console.WriteLine(projectedData.Last().Name);
85-
86-
var all = new
87-
{
88-
test1 = new List<int> { 1, 2, 3 }.ToDynamicList(typeof(int)),
89-
test2 = new List<dynamic> { 4, 5, 6 }.ToDynamicList(typeof(int)),
90-
test3 = new List<object> { 7, 8, 9 }.ToDynamicList(typeof(int))
71+
CustomTypeProvider = new TestCustomTypeProvider()
9172
};
92-
Console.WriteLine("all {0}", JsonConvert.SerializeObject(all, Formatting.Indented));
9373

94-
var anyTest = new[]
95-
{
96-
new { id = "1", values =new [] { 1, 2, 3 } },
97-
new { id = "2", values =new [] { 1, 4 } },
98-
new { id = "3", values =new [] { 9, 5 } }
99-
}.AsQueryable();
74+
//// Act
75+
//var testDataAsQueryable = new List<string> { "name1", "name2" }.AsQueryable();
76+
//var projectedData = (IQueryable<NestedDto>)testDataAsQueryable.Select(config, $"new {typeof(NestedDto).FullName}(~ as Name)");
77+
//Console.WriteLine(projectedData.First().Name);
78+
//Console.WriteLine(projectedData.Last().Name);
10079

101-
var any1 = anyTest.Where(x => x.values.Contains(1));
102-
Console.WriteLine("any1 {0}", JsonConvert.SerializeObject(any1, Formatting.Indented));
80+
//var all = new
81+
//{
82+
// test1 = new List<int> { 1, 2, 3 }.ToDynamicList(typeof(int)),
83+
// test2 = new List<dynamic> { 4, 5, 6 }.ToDynamicList(typeof(int)),
84+
// test3 = new List<object> { 7, 8, 9 }.ToDynamicList(typeof(int))
85+
//};
86+
//Console.WriteLine("all {0}", JsonConvert.SerializeObject(all, Formatting.Indented));
10387

104-
var any2 = anyTest.Where("values.Contains(1)");
105-
Console.WriteLine("any2 {0}", JsonConvert.SerializeObject(any2, Formatting.Indented));
88+
//var anyTest = new[]
89+
//{
90+
// new { id = "1", values =new [] { 1, 2, 3 } },
91+
// new { id = "2", values =new [] { 1, 4 } },
92+
// new { id = "3", values =new [] { 9, 5 } }
93+
//}.AsQueryable();
10694

95+
//var any1 = anyTest.Where(x => x.values.Contains(1));
96+
//Console.WriteLine("any1 {0}", JsonConvert.SerializeObject(any1, Formatting.Indented));
10797

98+
//var any2 = anyTest.Where("values.Contains(1)");
99+
//Console.WriteLine("any2 {0}", JsonConvert.SerializeObject(any2, Formatting.Indented));
108100

109-
var dateLastModified = new DateTime(2018, 1, 15);
101+
DateTime dateLastModified = new DateTime(2018, 1, 15);
110102

111103
var context = new TestContext();
112104
context.Cars.Add(new Car { Brand = "Ford", Color = "Blue", Vin = "yes", Year = "2017", DateLastModified = dateLastModified });
@@ -120,19 +112,48 @@ static void Main(string[] args)
120112
context.Brands.Add(new Brand { BrandType = "Alfa", BrandName = "Romeo" });
121113
context.SaveChanges();
122114

123-
context.BaseDtos.Add(new TestDto { BaseName = "b", Name = "t" });
124-
context.BaseDtos.Add(new OtherTestDto { BaseName = "b", Name = "t" });
115+
var testDto1 = new TestDto { BaseName = "a", Name = "t" };
116+
context.BaseDtos.Add(testDto1);
117+
var testDto2 = new TestDto { BaseName = "b", Name = "t" };
118+
context.BaseDtos.Add(testDto2);
119+
120+
var otherTestDto = new OtherTestDto { BaseName = "c", Name = "t" };
121+
context.BaseDtos.Add(otherTestDto);
122+
context.SaveChanges();
123+
124+
context.ComplexDtos.Add(new ComplexDto { X = "both", ListOfBaseDtos = new BaseDto[] { testDto1, otherTestDto } });
125+
context.ComplexDtos.Add(new ComplexDto { X = "testDto", ListOfBaseDtos = new BaseDto[] { testDto2 } });
125126
context.SaveChanges();
126127

127-
var oftypeTestDto1 = context.BaseDtos.OfType<TestDto>().Where(x => x.Name == "t").ToArray();
128-
var oftypeTestDto2 = context.BaseDtos.OfType<TestDto>().Where("Name == \"t\"").ToArray();
128+
OfTypeAndCastTests(context, config);
129129

130130
var carDateLastModified = context.Cars.Where(config, "DateLastModified > \"2018-01-16\"");
131131
Console.WriteLine("carDateLastModified {0}", JsonConvert.SerializeObject(carDateLastModified, Formatting.Indented));
132132

133-
//var carFirstOrDefault = context.Cars.Where(config, "Brand == \"Ford\"");
134-
//Console.WriteLine("carFirstOrDefault {0}", JsonConvert.SerializeObject(carFirstOrDefault, Formatting.Indented));
133+
var carFirstOrDefault = context.Cars.Where(config, "Brand == \"Ford\"");
134+
Console.WriteLine("carFirstOrDefault {0}", JsonConvert.SerializeObject(carFirstOrDefault, Formatting.Indented));
135+
136+
LikeTests(context, config);
137+
138+
var testDynamic = context.Cars.Select(c => new
139+
{
140+
K = c.Key,
141+
C = c.Color
142+
});
143+
144+
var testDynamicResult = testDynamic.Select("it").OrderBy("C");
145+
try
146+
{
147+
Console.WriteLine("resultX {0}", JsonConvert.SerializeObject(testDynamicResult, Formatting.Indented));
148+
}
149+
catch (Exception e)
150+
{
151+
Console.WriteLine(e);
152+
}
153+
}
135154

155+
private static void LikeTests(TestContext context, ParsingConfig config)
156+
{
136157
//var carsLike1 =
137158
// from c in context.Cars
138159
// where EF.Functions.Like(c.Brand, "%a%")
@@ -142,33 +163,66 @@ static void Main(string[] args)
142163
//var cars2Like = context.Cars.Where(c => EF.Functions.Like(c.Brand, "%a%"));
143164
//Console.WriteLine("cars2Like {0}", JsonConvert.SerializeObject(cars2Like, Formatting.Indented));
144165

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

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

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

154176
//var dynamicFunctionsLike2 = context.Cars.Where(config, "DynamicFunctions.Like(Vin, \"%a.%b%\", \".\")");
155-
//Console.WriteLine("dynamicFunctionsLike2 {0}", JsonConvert.SerializeObject(dynamicFunctionsLike2, Formatting.Indented));
177+
//Console.WriteLine("dynamicFunctionsLike2 {0}",
178+
//JsonConvert.SerializeObject(dynamicFunctionsLike2, Formatting.Indented));
179+
}
156180

157-
//var testDynamic = context.Cars.Select(c => new
158-
//{
159-
// K = c.Key,
160-
// C = c.Color
161-
//});
181+
private static void OfTypeAndCastTests(TestContext context, ParsingConfig config)
182+
{
183+
var cast = context.BaseDtos.Where(b => b is TestDto).Cast<TestDto>().ToArray();
184+
var castDynamicWithType = context.BaseDtos.Where(b => b is TestDto).Cast(typeof(TestDto)).ToDynamicArray();
185+
var castDynamicWithString = context.BaseDtos.Where(b => b is TestDto).Cast(config, "ConsoleAppEF2.Database.TestDto").ToDynamicArray();
162186

163-
//var testDynamicResult = testDynamic.Select("it").OrderBy("C");
164-
//try
165-
//{
166-
// Console.WriteLine("resultX {0}", JsonConvert.SerializeObject(testDynamicResult, Formatting.Indented));
167-
//}
168-
//catch (Exception e)
169-
//{
170-
// Console.WriteLine(e);
171-
//}
187+
var oftype = context.BaseDtos.OfType<TestDto>().ToArray();
188+
bool ofTypeAny = context.BaseDtos.OfType<TestDto>().Any();
189+
var oftypeDynamicWithType = context.BaseDtos.OfType(typeof(TestDto)).ToDynamicArray();
190+
var oftypeDynamicWithString = context.BaseDtos.OfType(config, "ConsoleAppEF2.Database.TestDto").ToDynamicArray();
191+
192+
var configX = new ParsingConfig
193+
{
194+
ResolveTypesBySimpleName = true
195+
};
196+
var oftypeDynamicWithSimpleNameString = context.BaseDtos.OfType(configX, "TestDto").ToDynamicArray();
197+
198+
int isOfType = context.BaseDtos.Count(b => b is TestDto);
199+
int isOfTypeDynamicTestDto = context.BaseDtos.Count(config, "is(\"ConsoleAppEF2.Database.TestDto\")");
200+
int isOfTypeDynamicOtherTestDto = context.BaseDtos.Count(config, "is(\"ConsoleAppEF2.Database.OtherTestDto\")");
201+
int isOfTypeDynamicComplexDto = context.BaseDtos.Count(config, "is(\"ConsoleAppEF2.Database.ComplexDto\")");
202+
203+
var asOfType = context.BaseDtos.Where(b => b as TestDto != null).ToArray();
204+
var asOfTypeDynamicTestDto = context.BaseDtos.Where(config, "As(\"ConsoleAppEF2.Database.TestDto\") != null").ToDynamicArray();
205+
var asOfTypeDynamicOtherTestDto = context.BaseDtos.Where(config, "As(\"ConsoleAppEF2.Database.OtherTestDto\") != null").ToDynamicArray();
206+
var asOfTypeDynamicComplexDto = context.BaseDtos.Where(config, "As(\"ConsoleAppEF2.Database.ComplexDto\") != null").ToDynamicArray();
207+
208+
var castOnX = context.BaseDtos.Where(b => b as TestDto != null).Where(b => ((TestDto)b).Name != null).ToArray();
209+
var castOnXDynamic = context.BaseDtos.Where(b => b as TestDto != null).Where(config, "Cast(\"ConsoleAppEF2.Database.TestDto\").Name != null").ToArray();
210+
211+
var oftypeTestDto = context.BaseDtos.OfType<TestDto>().Where(x => x.Name == "t").ToArray();
212+
var oftypeTestDtoDynamic = context.BaseDtos.OfType<TestDto>().Where("Name == \"t\"").ToArray();
213+
214+
var complexOfType = context.ComplexDtos.Select(c => c.ListOfBaseDtos.OfType<TestDto>().Where(x => x.Name == "t"))
215+
.ToArray();
216+
var complexOfTypeDynamic = context.ComplexDtos
217+
.Select(config, "ListOfBaseDtos.OfType(\"ConsoleAppEF2.Database.TestDto\").Where(Name == \"t\")")
218+
.ToDynamicArray();
219+
220+
var complexCast = context.ComplexDtos.Where(c => c.X == "testDto").ToList()
221+
.Select(c => c.ListOfBaseDtos.Cast<TestDto>().Where(x => x.Name == "t"))
222+
.ToArray();
223+
var complexCastDynamic = context.ComplexDtos.Where(c => c.X == "testDto").ToList().AsQueryable()
224+
.Select(config, "ListOfBaseDtos.Cast(\"ConsoleAppEF2.Database.TestDto\").Where(Name == \"t\")")
225+
.ToDynamicArray();
172226
}
173227
}
174228
}

0 commit comments

Comments
 (0)