Skip to content

Commit 44eadf7

Browse files
committed
NullableDemo (#111)
1 parent d96dccf commit 44eadf7

12 files changed

+278
-3
lines changed

System.Linq.Dynamic.Core.sln

+15
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test.NETCoreApp1.1.EntityFr
5555
EndProject
5656
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QueryBug", "src-console\QueryBug\QueryBug.csproj", "{D7181F75-05F3-4D26-A5ED-B649374B43E2}"
5757
EndProject
58+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NullableDemo", "src-console\NullableDemo\NullableDemo.csproj", "{8A9DF3BD-B75F-4220-91C1-08B9132C0A04}"
59+
EndProject
5860
Global
5961
GlobalSection(SolutionConfigurationPlatforms) = preSolution
6062
Debug|Any CPU = Debug|Any CPU
@@ -245,6 +247,18 @@ Global
245247
{D7181F75-05F3-4D26-A5ED-B649374B43E2}.Release|x64.Build.0 = Release|Any CPU
246248
{D7181F75-05F3-4D26-A5ED-B649374B43E2}.Release|x86.ActiveCfg = Release|Any CPU
247249
{D7181F75-05F3-4D26-A5ED-B649374B43E2}.Release|x86.Build.0 = Release|Any CPU
250+
{8A9DF3BD-B75F-4220-91C1-08B9132C0A04}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
251+
{8A9DF3BD-B75F-4220-91C1-08B9132C0A04}.Debug|Any CPU.Build.0 = Debug|Any CPU
252+
{8A9DF3BD-B75F-4220-91C1-08B9132C0A04}.Debug|x64.ActiveCfg = Debug|Any CPU
253+
{8A9DF3BD-B75F-4220-91C1-08B9132C0A04}.Debug|x64.Build.0 = Debug|Any CPU
254+
{8A9DF3BD-B75F-4220-91C1-08B9132C0A04}.Debug|x86.ActiveCfg = Debug|Any CPU
255+
{8A9DF3BD-B75F-4220-91C1-08B9132C0A04}.Debug|x86.Build.0 = Debug|Any CPU
256+
{8A9DF3BD-B75F-4220-91C1-08B9132C0A04}.Release|Any CPU.ActiveCfg = Release|Any CPU
257+
{8A9DF3BD-B75F-4220-91C1-08B9132C0A04}.Release|Any CPU.Build.0 = Release|Any CPU
258+
{8A9DF3BD-B75F-4220-91C1-08B9132C0A04}.Release|x64.ActiveCfg = Release|Any CPU
259+
{8A9DF3BD-B75F-4220-91C1-08B9132C0A04}.Release|x64.Build.0 = Release|Any CPU
260+
{8A9DF3BD-B75F-4220-91C1-08B9132C0A04}.Release|x86.ActiveCfg = Release|Any CPU
261+
{8A9DF3BD-B75F-4220-91C1-08B9132C0A04}.Release|x86.Build.0 = Release|Any CPU
248262
EndGlobalSection
249263
GlobalSection(SolutionProperties) = preSolution
250264
HideSolutionNode = FALSE
@@ -265,6 +279,7 @@ Global
265279
{DE991A5E-575C-4B0F-B93D-6D283E5928D6} = {7971CAEB-B9F2-416B-966D-2D697C4C1E62}
266280
{F65A54BA-7C83-44DE-B67D-51065D5426F4} = {7971CAEB-B9F2-416B-966D-2D697C4C1E62}
267281
{D7181F75-05F3-4D26-A5ED-B649374B43E2} = {7971CAEB-B9F2-416B-966D-2D697C4C1E62}
282+
{8A9DF3BD-B75F-4220-91C1-08B9132C0A04} = {7971CAEB-B9F2-416B-966D-2D697C4C1E62}
268283
EndGlobalSection
269284
GlobalSection(ExtensibilityGlobals) = postSolution
270285
SolutionGuid = {94C56722-194E-4B8B-BC23-B3F754E89A20}

src-console/NullableDemo/App.config

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
5+
</startup>
6+
</configuration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{8A9DF3BD-B75F-4220-91C1-08B9132C0A04}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>NullableDemo</RootNamespace>
11+
<AssemblyName>NullableDemo</AssemblyName>
12+
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
37+
<HintPath>..\..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
38+
</Reference>
39+
<Reference Include="System" />
40+
<Reference Include="System.Core" />
41+
<Reference Include="System.Xml.Linq" />
42+
<Reference Include="System.Data.DataSetExtensions" />
43+
<Reference Include="Microsoft.CSharp" />
44+
<Reference Include="System.Data" />
45+
<Reference Include="System.Net.Http" />
46+
<Reference Include="System.Xml" />
47+
</ItemGroup>
48+
<ItemGroup>
49+
<Compile Include="Person.cs" />
50+
<Compile Include="Pet.cs" />
51+
<Compile Include="Program.cs" />
52+
<Compile Include="Properties\AssemblyInfo.cs" />
53+
<Compile Include="Test.cs" />
54+
<Compile Include="Toy.cs" />
55+
</ItemGroup>
56+
<ItemGroup>
57+
<None Include="App.config" />
58+
<None Include="packages.config" />
59+
</ItemGroup>
60+
<ItemGroup>
61+
<ProjectReference Include="..\..\src\System.Linq.Dynamic.Core\System.Linq.Dynamic.Core.csproj">
62+
<Project>{b70cd050-3b9c-406c-85fd-1a26394ec7c8}</Project>
63+
<Name>System.Linq.Dynamic.Core</Name>
64+
</ProjectReference>
65+
</ItemGroup>
66+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
67+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
68+
Other similar extension points exist, see Microsoft.Common.targets.
69+
<Target Name="BeforeBuild">
70+
</Target>
71+
<Target Name="AfterBuild">
72+
</Target>
73+
-->
74+
</Project>

src-console/NullableDemo/Person.cs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
3+
namespace NullableDemo
4+
{
5+
public class Person
6+
{
7+
public Guid Id;
8+
9+
public string Name;
10+
11+
public int From;
12+
13+
public int Till;
14+
}
15+
}

src-console/NullableDemo/Pet.cs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
3+
namespace NullableDemo
4+
{
5+
public class Pet
6+
{
7+
public Guid Id;
8+
9+
public string Name;
10+
11+
public Person Owner;
12+
13+
public Guid? OwnerId => Owner.Id;
14+
15+
public int From;
16+
17+
public int Till;
18+
}
19+
}

src-console/NullableDemo/Program.cs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace NullableDemo
2+
{
3+
class Program
4+
{
5+
static void Main(string[] args)
6+
{
7+
Test t = new Test();
8+
t.Join();
9+
}
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Reflection;
2+
using System.Runtime.InteropServices;
3+
4+
// General Information about an assembly is controlled through the following
5+
// set of attributes. Change these attribute values to modify the information
6+
// associated with an assembly.
7+
[assembly: AssemblyTitle("NullableDemo")]
8+
[assembly: AssemblyDescription("")]
9+
[assembly: AssemblyConfiguration("")]
10+
[assembly: AssemblyCompany("")]
11+
[assembly: AssemblyProduct("NullableDemo")]
12+
[assembly: AssemblyCopyright("Copyright © 2017")]
13+
[assembly: AssemblyTrademark("")]
14+
[assembly: AssemblyCulture("")]
15+
16+
// Setting ComVisible to false makes the types in this assembly not visible
17+
// to COM components. If you need to access a type in this assembly from
18+
// COM, set the ComVisible attribute to true on that type.
19+
[assembly: ComVisible(false)]
20+
21+
// The following GUID is for the ID of the typelib if this project is exposed to COM
22+
[assembly: Guid("de991a5e-575c-4b0f-b93d-6d283e5928d6")]
23+
24+
// Version information for an assembly consists of the following four values:
25+
//
26+
// Major Version
27+
// Minor Version
28+
// Build Number
29+
// Revision
30+
//
31+
// You can specify all the values or you can default the Build and Revision Numbers
32+
// by using the '*' as shown below:
33+
// [assembly: AssemblyVersion("1.0.*")]
34+
[assembly: AssemblyVersion("1.0.0.0")]
35+
[assembly: AssemblyFileVersion("1.0.0.0")]

src-console/NullableDemo/Test.cs

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Linq.Dynamic.Core;
5+
using Newtonsoft.Json;
6+
7+
namespace NullableDemo
8+
{
9+
public class Test
10+
{
11+
public void Join()
12+
{
13+
// Arrange
14+
Person magnus = new Person { Id = Guid.NewGuid(), Name = "Hedlund, Magnus", From = 100, Till = 1000 };
15+
Pet daisy = new Pet { Id = Guid.NewGuid(), Name = "Daisy", Owner = magnus, From = 27, Till = 1200 };
16+
IList<Person> people = new List<Person> { magnus };
17+
IList<Pet> pets = new List<Pet> { daisy };
18+
19+
// Act
20+
Act(people, pets);
21+
}
22+
23+
private dynamic[] Act(IList<Person> peopleList, IList<Pet> petsList)
24+
{
25+
var peopleQuery = peopleList.AsQueryable().Where(p => p.From < 111 && p.Till > 444);
26+
var petsQuery = petsList.AsQueryable().Where(p => p.From < 66 && p.Till > 888);
27+
28+
var query = peopleQuery.Join(petsQuery, people => new { firstKey = (Guid?) people.Id }, pet => new { firstKey = pet.OwnerId }, (inner, outer) => new { pet = inner, person = outer })
29+
.Select(res => new { personName = res.person.Name, petName = res.pet.Name });
30+
31+
var queryArray = query.ToArray();
32+
foreach (var x in queryArray)
33+
{
34+
Console.WriteLine(JsonConvert.SerializeObject(x));
35+
}
36+
Console.WriteLine(new string('-', 80));
37+
38+
var dynamicPeopleQuery1 = peopleList.AsQueryable().Where($"From < {111} AND Till > {444}");
39+
var dynamicPetsQuery1 = petsList.AsQueryable().Where($"From < {66} AND Till > {888}");
40+
41+
var dynamicQuery1 = dynamicPeopleQuery1.Join(dynamicPetsQuery1, "new (Id as firstKey)", "new (Id as firstKey)", "new (inner as pet, outer as person)")
42+
.Select("new (person.Name as personName, pet.Name as petName)");
43+
44+
var dynamicQueryArray1 = dynamicQuery1.ToDynamicArray();
45+
foreach (var x in dynamicQueryArray1)
46+
{
47+
Console.WriteLine(JsonConvert.SerializeObject(x));
48+
}
49+
Console.WriteLine(new string('-', 80));
50+
51+
var dynamicPeopleQuery2 = peopleList.AsQueryable().Where($"From < {111} AND Till > {444}");
52+
var dynamicPetsQuery2 = petsList.AsQueryable().Where($"From < {66} AND Till > {888}");
53+
54+
var dynamicQuery2 = dynamicPeopleQuery2.Join(dynamicPetsQuery2, "new (Guid?(Id) as firstKey)", "new (OwnerId as firstKey)", "new (inner as pet, outer as person)")
55+
.Select("new (person.Name as personName, pet.Name as petName)");
56+
57+
var dynamicQueryArray2 = dynamicQuery2.ToDynamicArray();
58+
foreach (var x in dynamicQueryArray2)
59+
{
60+
Console.WriteLine(JsonConvert.SerializeObject(x));
61+
}
62+
Console.WriteLine(new string('-', 80));
63+
64+
return dynamicQueryArray2;
65+
}
66+
}
67+
}

src-console/NullableDemo/Toy.cs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
namespace NullableDemo
4+
{
5+
public class Toy
6+
{
7+
public Guid Id;
8+
9+
public int From;
10+
11+
public int Till;
12+
13+
public Pet Pet;
14+
15+
public Guid PetId => Pet.Id;
16+
}
17+
}
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net452" />
4+
</packages>

src/System.Linq.Dynamic.Core/DynamicClassFactory.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,17 @@ public static class DynamicClassFactory
6262

6363
private static int _index = -1;
6464

65+
/// <summary>
66+
/// The AssemblyName
67+
/// </summary>
68+
public static string DynamicAssemblyName = "System.Linq.Dynamic.Core.DynamicClasses, Version=1.0.0.0";
69+
6570
/// <summary>
6671
/// Initializes the <see cref="DynamicClassFactory"/> class.
6772
/// </summary>
6873
static DynamicClassFactory()
6974
{
70-
var assemblyName = new AssemblyName("System.Linq.Dynamic.Core.DynamicClasses, Version=1.0.0.0");
75+
var assemblyName = new AssemblyName(DynamicAssemblyName);
7176
var assemblyBuilder = AssemblyBuilderFactory.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
7277

7378
ModuleBuilder = assemblyBuilder.DefineDynamicModule("System.Linq.Dynamic.Core.DynamicClasses");
@@ -382,4 +387,4 @@ private static string Escape(string str)
382387
}
383388
}
384389
}
385-
#endif
390+
#endif

src/System.Linq.Dynamic.Core/DynamicQueryableExtensions.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,13 @@ private static void CheckOuterAndInnerTypes(bool createParameterCtor, Type outer
16571657
// If types are not the same, try to convert to Nullable and generate new LambdaExpression
16581658
if (outerSelectorReturnType != innerSelectorReturnType)
16591659
{
1660+
//var outerSelectorReturnTypeInfo = outerSelectorReturnType.GetTypeInfo();
1661+
//var innerSelectorReturnTypeInfo = innerSelectorReturnType.GetTypeInfo();
1662+
//if (outerSelectorReturnTypeInfo.BaseType == typeof(DynamicClass) && innerSelectorReturnTypeInfo.BaseType == typeof(DynamicClass))
1663+
//{
1664+
1665+
//}
1666+
16601667
if (ExpressionParser.IsNullableType(outerSelectorReturnType) && !ExpressionParser.IsNullableType(innerSelectorReturnType))
16611668
{
16621669
innerSelectorReturnType = ExpressionParser.ToNullableType(innerSelectorReturnType);
@@ -1671,7 +1678,7 @@ private static void CheckOuterAndInnerTypes(bool createParameterCtor, Type outer
16711678
// If types are still not the same, throw an Exception
16721679
if (outerSelectorReturnType != innerSelectorReturnType)
16731680
{
1674-
throw new ParseException(string.Format(CultureInfo.CurrentCulture, Res.IncompatibleTypes, outerType, innerType), -1);
1681+
throw new ParseException(string.Format(CultureInfo.CurrentCulture, Res.IncompatibleTypes, outerSelectorReturnType, innerSelectorReturnType), -1);
16751682
}
16761683
}
16771684
}

0 commit comments

Comments
 (0)