Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
</PropertyGroup>

<PropertyGroup>
<VersionPrefix>1.0.10</VersionPrefix>
<VersionPrefix>1.0.11</VersionPrefix>
</PropertyGroup>

<Choose>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<Compile Include="..\ConsoleAppEF2.0\Database\BaseDto.cs" Link="Database\BaseDto.cs" />
<Compile Include="..\ConsoleAppEF2.0\Database\TestDto.cs" Link="Database\TestDto.cs" />
<Compile Include="..\ConsoleAppEF2.0\Database\OtherTestDto.cs" Link="Database\OtherTestDto.cs" />
<Compile Include="..\ConsoleAppEF2.0\Database\ComplexDto.cs" Link="Database\ComplexDto.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class TestContext : DbContext

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

public virtual DbSet<ComplexDto> ComplexDtos { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseLoggerFactory(MyLoggerFactory); // Warning: Do not create a new ILoggerFactory instance each time
Expand All @@ -27,6 +29,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<Car>().HasKey(c => c.Key);
modelBuilder.Entity<Brand>().HasKey(b => b.BrandType);
modelBuilder.Entity<BaseDto>().HasKey(t => t.Key);
modelBuilder.Entity<ComplexDto>().HasKey(t => t.Key);
}

// https://stackoverflow.com/questions/46212704/how-do-i-write-ef-functions-extension-method
Expand Down
6 changes: 6 additions & 0 deletions src-console/ConsoleAppEF2.0.2_InMemory/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public Type ResolveType(string typeName)

return ResolveType(assemblies, typeName);
}

public Type ResolveTypeBySimpleName(string typeName)
{
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
return ResolveTypeBySimpleName(assemblies, typeName);
}
}

private static IQueryable GetQueryable()
Expand Down
15 changes: 15 additions & 0 deletions src-console/ConsoleAppEF2.0/Database/ComplexDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace ConsoleAppEF2.Database
{
public class ComplexDto
{
[Key]
public int Key { get; set; }

public string X { get; set; }

public IEnumerable<BaseDto> ListOfBaseDtos { get; set; }
}
}
6 changes: 6 additions & 0 deletions src-console/ConsoleAppEF2.0/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ public Type ResolveType(string typeName)
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
return ResolveType(assemblies, typeName);
}

public Type ResolveTypeBySimpleName(string typeName)
{
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
return ResolveTypeBySimpleName(assemblies, typeName);
}
}

private static IQueryable GetQueryable()
Expand Down
6 changes: 6 additions & 0 deletions src-console/ConsoleAppEF2.1.1/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ public Type ResolveType(string typeName)

return ResolveType(assemblies, typeName);
}

public Type ResolveTypeBySimpleName(string typeName)
{
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
return ResolveTypeBySimpleName(assemblies, typeName);
}
}

private static IQueryable GetQueryable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<Compile Include="..\ConsoleAppEF2.0\Database\BaseDto.cs" Link="Database\BaseDto.cs" />
<Compile Include="..\ConsoleAppEF2.0\Database\TestDto.cs" Link="Database\TestDto.cs" />
<Compile Include="..\ConsoleAppEF2.0\Database\OtherTestDto.cs" Link="Database\OtherTestDto.cs" />
<Compile Include="..\ConsoleAppEF2.0\Database\ComplexDto.cs" Link="Database\ComplexDto.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
196 changes: 125 additions & 71 deletions src-console/ConsoleAppEF2.1.1_InMemory/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public class NestedDto3
public int Id { get; set; }
}

class NetCore21CustomTypeProvider : AbstractDynamicLinqCustomTypeProvider, IDynamicLinkCustomTypeProvider
class TestCustomTypeProvider : DefaultDynamicLinqCustomTypeProvider, IDynamicLinkCustomTypeProvider
{
public HashSet<Type> GetCustomTypes()
public new HashSet<Type> GetCustomTypes()
{
var assemblies = AppDomain.CurrentDomain.GetAssemblies();

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

return set;
}

public Type ResolveType(string typeName)
{
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
return ResolveType(assemblies, typeName);
}
}

static void Main(string[] args)
{
var q = new[] { new NestedDto(), new NestedDto { NestedDto2 = new NestedDto2 { NestedDto3 = new NestedDto3 { Id = 42 } } } }.AsQueryable();
//var q = new[] { new NestedDto(), new NestedDto { NestedDto2 = new NestedDto2 { NestedDto3 = new NestedDto3 { Id = 42 } } } }.AsQueryable();

var np1 = q.Select("np(it.NestedDto2.NestedDto3.Id, 0)");
var npResult1 = np1.ToDynamicList<int>();
Console.WriteLine("npResult1 {0}", JsonConvert.SerializeObject(npResult1, Formatting.Indented));
//var np1 = q.Select("np(it.NestedDto2.NestedDto3.Id, 0)");
//var npResult1 = np1.ToDynamicList<int>();
//Console.WriteLine("npResult1 {0}", JsonConvert.SerializeObject(npResult1, Formatting.Indented));

var np2 = q.Select("np(it.NestedDto2.NestedDto3.Id)");
var npResult2 = np2.ToDynamicList<int?>();
Console.WriteLine("npResult2 {0}", JsonConvert.SerializeObject(npResult2, Formatting.Indented));
//var np2 = q.Select("np(it.NestedDto2.NestedDto3.Id)");
//var npResult2 = np2.ToDynamicList<int?>();
//Console.WriteLine("npResult2 {0}", JsonConvert.SerializeObject(npResult2, Formatting.Indented));

var r1 = q.Select("it != null && it.NestedDto2 != null ? it.NestedDto2.Id : null");
var list1 = r1.ToDynamicList<int?>();
//var r1 = q.Select("it != null && it.NestedDto2 != null ? it.NestedDto2.Id : null");
//var list1 = r1.ToDynamicList<int?>();

var r2 = q.Select("it != null && it.NestedDto2 != null ? it.NestedDto2 : null");
var list2 = r2.ToDynamicList<NestedDto2>();
//var r2 = q.Select("it != null && it.NestedDto2 != null ? it.NestedDto2 : null");
//var list2 = r2.ToDynamicList<NestedDto2>();

var config = new ParsingConfig
{
AllowNewToEvaluateAnyType = true,
CustomTypeProvider = new NetCore21CustomTypeProvider()
};

// Act
var testDataAsQueryable = new List<string> { "name1", "name2" }.AsQueryable();
var projectedData = (IQueryable<NestedDto>)testDataAsQueryable.Select(config, $"new {typeof(NestedDto).FullName}(~ as Name)");
Console.WriteLine(projectedData.First().Name);
Console.WriteLine(projectedData.Last().Name);

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))
CustomTypeProvider = new TestCustomTypeProvider()
};
Console.WriteLine("all {0}", JsonConvert.SerializeObject(all, Formatting.Indented));

var anyTest = new[]
{
new { id = "1", values =new [] { 1, 2, 3 } },
new { id = "2", values =new [] { 1, 4 } },
new { id = "3", values =new [] { 9, 5 } }
}.AsQueryable();
//// Act
//var testDataAsQueryable = new List<string> { "name1", "name2" }.AsQueryable();
//var projectedData = (IQueryable<NestedDto>)testDataAsQueryable.Select(config, $"new {typeof(NestedDto).FullName}(~ as Name)");
//Console.WriteLine(projectedData.First().Name);
//Console.WriteLine(projectedData.Last().Name);

var any1 = anyTest.Where(x => x.values.Contains(1));
Console.WriteLine("any1 {0}", JsonConvert.SerializeObject(any1, Formatting.Indented));
//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.Indented));

var any2 = anyTest.Where("values.Contains(1)");
Console.WriteLine("any2 {0}", JsonConvert.SerializeObject(any2, Formatting.Indented));
//var anyTest = new[]
//{
// new { id = "1", values =new [] { 1, 2, 3 } },
// new { id = "2", values =new [] { 1, 4 } },
// new { id = "3", values =new [] { 9, 5 } }
//}.AsQueryable();

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

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

var dateLastModified = new DateTime(2018, 1, 15);
DateTime dateLastModified = new DateTime(2018, 1, 15);

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

context.BaseDtos.Add(new TestDto { BaseName = "b", Name = "t" });
context.BaseDtos.Add(new OtherTestDto { BaseName = "b", Name = "t" });
var testDto1 = new TestDto { BaseName = "a", Name = "t" };
context.BaseDtos.Add(testDto1);
var testDto2 = new TestDto { BaseName = "b", Name = "t" };
context.BaseDtos.Add(testDto2);

var otherTestDto = new OtherTestDto { BaseName = "c", Name = "t" };
context.BaseDtos.Add(otherTestDto);
context.SaveChanges();

context.ComplexDtos.Add(new ComplexDto { X = "both", ListOfBaseDtos = new BaseDto[] { testDto1, otherTestDto } });
context.ComplexDtos.Add(new ComplexDto { X = "testDto", ListOfBaseDtos = new BaseDto[] { testDto2 } });
context.SaveChanges();

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

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

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

LikeTests(context, config);

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);
}
}

private static void LikeTests(TestContext context, ParsingConfig config)
{
//var carsLike1 =
// from c in context.Cars
// where EF.Functions.Like(c.Brand, "%a%")
Expand All @@ -142,33 +163,66 @@ static void Main(string[] args)
//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 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 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));
//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));
//Console.WriteLine("dynamicFunctionsLike2 {0}",
//JsonConvert.SerializeObject(dynamicFunctionsLike2, Formatting.Indented));
}

//var testDynamic = context.Cars.Select(c => new
//{
// K = c.Key,
// C = c.Color
//});
private static void OfTypeAndCastTests(TestContext context, ParsingConfig config)
{
var cast = context.BaseDtos.Where(b => b is TestDto).Cast<TestDto>().ToArray();
var castDynamicWithType = context.BaseDtos.Where(b => b is TestDto).Cast(typeof(TestDto)).ToDynamicArray();
var castDynamicWithString = context.BaseDtos.Where(b => b is TestDto).Cast(config, "ConsoleAppEF2.Database.TestDto").ToDynamicArray();

//var testDynamicResult = testDynamic.Select("it").OrderBy("C");
//try
//{
// Console.WriteLine("resultX {0}", JsonConvert.SerializeObject(testDynamicResult, Formatting.Indented));
//}
//catch (Exception e)
//{
// Console.WriteLine(e);
//}
var oftype = context.BaseDtos.OfType<TestDto>().ToArray();
bool ofTypeAny = context.BaseDtos.OfType<TestDto>().Any();
var oftypeDynamicWithType = context.BaseDtos.OfType(typeof(TestDto)).ToDynamicArray();
var oftypeDynamicWithString = context.BaseDtos.OfType(config, "ConsoleAppEF2.Database.TestDto").ToDynamicArray();

var configX = new ParsingConfig
{
ResolveTypesBySimpleName = true
};
var oftypeDynamicWithSimpleNameString = context.BaseDtos.OfType(configX, "TestDto").ToDynamicArray();

int isOfType = context.BaseDtos.Count(b => b is TestDto);
int isOfTypeDynamicTestDto = context.BaseDtos.Count(config, "is(\"ConsoleAppEF2.Database.TestDto\")");
int isOfTypeDynamicOtherTestDto = context.BaseDtos.Count(config, "is(\"ConsoleAppEF2.Database.OtherTestDto\")");
int isOfTypeDynamicComplexDto = context.BaseDtos.Count(config, "is(\"ConsoleAppEF2.Database.ComplexDto\")");

var asOfType = context.BaseDtos.Where(b => b as TestDto != null).ToArray();
var asOfTypeDynamicTestDto = context.BaseDtos.Where(config, "As(\"ConsoleAppEF2.Database.TestDto\") != null").ToDynamicArray();
var asOfTypeDynamicOtherTestDto = context.BaseDtos.Where(config, "As(\"ConsoleAppEF2.Database.OtherTestDto\") != null").ToDynamicArray();
var asOfTypeDynamicComplexDto = context.BaseDtos.Where(config, "As(\"ConsoleAppEF2.Database.ComplexDto\") != null").ToDynamicArray();

var castOnX = context.BaseDtos.Where(b => b as TestDto != null).Where(b => ((TestDto)b).Name != null).ToArray();
var castOnXDynamic = context.BaseDtos.Where(b => b as TestDto != null).Where(config, "Cast(\"ConsoleAppEF2.Database.TestDto\").Name != null").ToArray();

var oftypeTestDto = context.BaseDtos.OfType<TestDto>().Where(x => x.Name == "t").ToArray();
var oftypeTestDtoDynamic = context.BaseDtos.OfType<TestDto>().Where("Name == \"t\"").ToArray();

var complexOfType = context.ComplexDtos.Select(c => c.ListOfBaseDtos.OfType<TestDto>().Where(x => x.Name == "t"))
.ToArray();
var complexOfTypeDynamic = context.ComplexDtos
.Select(config, "ListOfBaseDtos.OfType(\"ConsoleAppEF2.Database.TestDto\").Where(Name == \"t\")")
.ToDynamicArray();

var complexCast = context.ComplexDtos.Where(c => c.X == "testDto").ToList()
.Select(c => c.ListOfBaseDtos.Cast<TestDto>().Where(x => x.Name == "t"))
.ToArray();
var complexCastDynamic = context.ComplexDtos.Where(c => c.X == "testDto").ToList().AsQueryable()
.Select(config, "ListOfBaseDtos.Cast(\"ConsoleAppEF2.Database.TestDto\").Where(Name == \"t\")")
.ToDynamicArray();
}
}
}
Loading