├── .gitignore ├── LICENSE ├── README.md └── src ├── EntityFramework.MemoryJoin.TestRunner45 ├── App.config ├── DAL │ ├── Address.cs │ ├── Price.cs │ └── SampleContext.cs ├── EntityFramework.MemoryJoin.TestRunner45.csproj ├── Program.cs ├── Properties │ └── AssemblyInfo.cs └── packages.config ├── EntityFramework.MemoryJoin.sln ├── EntityFramework.MemoryJoin ├── App.config ├── EntityFramework.MemoryJoin.csproj ├── GlobalSuppressions.cs ├── Internal │ ├── EFHelper.cs │ ├── InterceptionOptions.cs │ ├── KnownProvider.cs │ ├── Mapping.cs │ ├── MappingHelper.cs │ └── ValuesInjectionMethodInternal.cs ├── MemoryJoiner.cs ├── MemoryJoinerInterceptor.cs ├── Properties │ └── AssemblyInfo.cs ├── QueryModelClass.cs ├── ValuesInjectionMethod.cs └── packages.config ├── EntityFrameworkCore.MemoryJoin.Ef6 └── EntityFrameworkCore.MemoryJoin.Ef6.csproj ├── EntityFrameworkCore.MemoryJoin.IntegrationTests ├── EntityFrameworkCore.MemoryJoin.IntegrationTests.csproj ├── SqliteTests.cs ├── TestModels │ ├── TestDbContext.cs │ ├── TestEntity.cs │ └── TestInMemoryEntity.cs └── Utils │ ├── TestLogger.cs │ └── TestLoggerFactory.cs ├── EntityFrameworkCore.MemoryJoin.TestRunnerCore ├── DAL │ ├── Address.cs │ └── SampleContext.cs ├── EntityFrameworkCore.MemoryJoin.TestRunnerCore.csproj └── Program.cs ├── EntityFrameworkCore.MemoryJoin.TestRunnerEf6 ├── App.config └── EntityFrameworkCore.MemoryJoin.TestRunnerEf6.csproj ├── EntityFrameworkCore.MemoryJoin.TestRunnerNet5 └── EntityFrameworkCore.MemoryJoin.TestRunnerNet5.csproj └── EntityFrameworkCore.MemoryJoin ├── EntityFrameworkCore.MemoryJoin.csproj ├── Internal └── EFHelper.cs ├── MemoryJoiner.cs ├── QueryModelClass.cs └── ValuesInjectionMethod.cs /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.suo 8 | *.user 9 | *.userosscache 10 | *.sln.docstates 11 | 12 | # User-specific files (MonoDevelop/Xamarin Studio) 13 | *.userprefs 14 | 15 | # Build results 16 | [Dd]ebug/ 17 | [Dd]ebugPublic/ 18 | [Rr]elease/ 19 | [Rr]eleases/ 20 | x64/ 21 | x86/ 22 | bld/ 23 | [Bb]in/ 24 | [Oo]bj/ 25 | [Ll]og/ 26 | 27 | # Visual Studio 2015 cache/options directory 28 | .vs/ 29 | # Uncomment if you have tasks that create the project's static files in wwwroot 30 | #wwwroot/ 31 | 32 | # MSTest test Results 33 | [Tt]est[Rr]esult*/ 34 | [Bb]uild[Ll]og.* 35 | 36 | # NUNIT 37 | *.VisualState.xml 38 | TestResult.xml 39 | 40 | # Build Results of an ATL Project 41 | [Dd]ebugPS/ 42 | [Rr]eleasePS/ 43 | dlldata.c 44 | 45 | # .NET Core 46 | project.lock.json 47 | project.fragment.lock.json 48 | artifacts/ 49 | **/Properties/launchSettings.json 50 | 51 | *_i.c 52 | *_p.c 53 | *_i.h 54 | *.ilk 55 | *.meta 56 | *.obj 57 | *.pch 58 | *.pdb 59 | *.pgc 60 | *.pgd 61 | *.rsp 62 | *.sbr 63 | *.tlb 64 | *.tli 65 | *.tlh 66 | *.tmp 67 | *.tmp_proj 68 | *.log 69 | *.vspscc 70 | *.vssscc 71 | .builds 72 | *.pidb 73 | *.svclog 74 | *.scc 75 | 76 | # Chutzpah Test files 77 | _Chutzpah* 78 | 79 | # Visual C++ cache files 80 | ipch/ 81 | *.aps 82 | *.ncb 83 | *.opendb 84 | *.opensdf 85 | *.sdf 86 | *.cachefile 87 | *.VC.db 88 | *.VC.VC.opendb 89 | 90 | # Visual Studio profiler 91 | *.psess 92 | *.vsp 93 | *.vspx 94 | *.sap 95 | 96 | # TFS 2012 Local Workspace 97 | $tf/ 98 | 99 | # Guidance Automation Toolkit 100 | *.gpState 101 | 102 | # ReSharper is a .NET coding add-in 103 | _ReSharper*/ 104 | *.[Rr]e[Ss]harper 105 | *.DotSettings.user 106 | 107 | # JustCode is a .NET coding add-in 108 | .JustCode 109 | 110 | # TeamCity is a build add-in 111 | _TeamCity* 112 | 113 | # DotCover is a Code Coverage Tool 114 | *.dotCover 115 | 116 | # Visual Studio code coverage results 117 | *.coverage 118 | *.coveragexml 119 | 120 | # NCrunch 121 | _NCrunch_* 122 | .*crunch*.local.xml 123 | nCrunchTemp_* 124 | 125 | # MightyMoose 126 | *.mm.* 127 | AutoTest.Net/ 128 | 129 | # Web workbench (sass) 130 | .sass-cache/ 131 | 132 | # Installshield output folder 133 | [Ee]xpress/ 134 | 135 | # DocProject is a documentation generator add-in 136 | DocProject/buildhelp/ 137 | DocProject/Help/*.HxT 138 | DocProject/Help/*.HxC 139 | DocProject/Help/*.hhc 140 | DocProject/Help/*.hhk 141 | DocProject/Help/*.hhp 142 | DocProject/Help/Html2 143 | DocProject/Help/html 144 | 145 | # Click-Once directory 146 | publish/ 147 | 148 | # Publish Web Output 149 | *.[Pp]ublish.xml 150 | *.azurePubxml 151 | # TODO: Comment the next line if you want to checkin your web deploy settings 152 | # but database connection strings (with potential passwords) will be unencrypted 153 | *.pubxml 154 | *.publishproj 155 | 156 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 157 | # checkin your Azure Web App publish settings, but sensitive information contained 158 | # in these scripts will be unencrypted 159 | PublishScripts/ 160 | 161 | # NuGet Packages 162 | *.nupkg 163 | # The packages folder can be ignored because of Package Restore 164 | **/packages/* 165 | # except build/, which is used as an MSBuild target. 166 | !**/packages/build/ 167 | # Uncomment if necessary however generally it will be regenerated when needed 168 | #!**/packages/repositories.config 169 | # NuGet v3's project.json files produces more ignorable files 170 | *.nuget.props 171 | *.nuget.targets 172 | 173 | # Microsoft Azure Build Output 174 | csx/ 175 | *.build.csdef 176 | 177 | # Microsoft Azure Emulator 178 | ecf/ 179 | rcf/ 180 | 181 | # Windows Store app package directories and files 182 | AppPackages/ 183 | BundleArtifacts/ 184 | Package.StoreAssociation.xml 185 | _pkginfo.txt 186 | 187 | # Visual Studio cache files 188 | # files ending in .cache can be ignored 189 | *.[Cc]ache 190 | # but keep track of directories ending in .cache 191 | !*.[Cc]ache/ 192 | 193 | # Others 194 | ClientBin/ 195 | ~$* 196 | *~ 197 | *.dbmdl 198 | *.dbproj.schemaview 199 | *.jfm 200 | *.pfx 201 | *.publishsettings 202 | orleans.codegen.cs 203 | 204 | # Since there are multiple workflows, uncomment next line to ignore bower_components 205 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 206 | #bower_components/ 207 | 208 | # RIA/Silverlight projects 209 | Generated_Code/ 210 | 211 | # Backup & report files from converting an old project file 212 | # to a newer Visual Studio version. Backup files are not needed, 213 | # because we have git ;-) 214 | _UpgradeReport_Files/ 215 | Backup*/ 216 | UpgradeLog*.XML 217 | UpgradeLog*.htm 218 | 219 | # SQL Server files 220 | *.mdf 221 | *.ldf 222 | *.ndf 223 | 224 | # Business Intelligence projects 225 | *.rdl.data 226 | *.bim.layout 227 | *.bim_*.settings 228 | 229 | # Microsoft Fakes 230 | FakesAssemblies/ 231 | 232 | # GhostDoc plugin setting file 233 | *.GhostDoc.xml 234 | 235 | # Node.js Tools for Visual Studio 236 | .ntvs_analysis.dat 237 | node_modules/ 238 | 239 | # Typescript v1 declaration files 240 | typings/ 241 | 242 | # Visual Studio 6 build log 243 | *.plg 244 | 245 | # Visual Studio 6 workspace options file 246 | *.opt 247 | 248 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 249 | *.vbw 250 | 251 | # Visual Studio LightSwitch build output 252 | **/*.HTMLClient/GeneratedArtifacts 253 | **/*.DesktopClient/GeneratedArtifacts 254 | **/*.DesktopClient/ModelManifest.xml 255 | **/*.Server/GeneratedArtifacts 256 | **/*.Server/ModelManifest.xml 257 | _Pvt_Extensions 258 | 259 | # Paket dependency manager 260 | .paket/paket.exe 261 | paket-files/ 262 | 263 | # FAKE - F# Make 264 | .fake/ 265 | 266 | # JetBrains Rider 267 | .idea/ 268 | *.sln.iml 269 | 270 | # CodeRush 271 | .cr/ 272 | 273 | # Python Tools for Visual Studio (PTVS) 274 | __pycache__/ 275 | *.pyc 276 | 277 | # Cake - Uncomment if you are using it 278 | # tools/** 279 | # !tools/packages.config 280 | 281 | # Telerik's JustMock configuration file 282 | *.jmconfig 283 | 284 | # BizTalk build output 285 | *.btp.cs 286 | *.btm.cs 287 | *.odx.cs 288 | *.xsd.cs 289 | /src/CreatePackage.bat 290 | /src/Package.nuspec 291 | /src/lib/net45/*.dll 292 | /src/lib/netstandard2.0/*.dll 293 | /src/lib/netstandard2.1/*.dll 294 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Anton Shkuratov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # EntityFrameworkCore.MemoryJoin 2 | Extension for EntityFramework for joins to in-memory data. Both Entity Framework 6 and Entity Framework Core are supported! 3 | Used SQL standard syntax. 4 | 5 | Tested with: MSSQL, PostgreSQL, and SQLite. (others should also work as standard EF API and SQL standard syntax are used) 6 | 7 | ## Usage 8 | 9 | 1. Internally MemoryJoin uses intermediate class for making queries. So you can either use your own or basically use built-in one. Like this: 10 | 11 | ```protected DbSet QueryData { get; set; }``` 12 | 13 | Or like this for EF Core 14 | 15 | ```protected DbSet QueryData { get; set; }``` 16 | 17 | Please note this DbSet is protected, so it can't be used by anybody, only MemoryJoin will access it. 18 | Another note: table for QueryModelClass is NOT required. It is used for internal mapping only. So if you use migrations - basically use -IgnoreChanges flag. 19 | 20 | 21 | 22 | 2. After DbSet is defined you can write as follows: 23 | 24 | ```using EntityFramework.MemoryJoin``` 25 | 26 | For EF Core: 27 | 28 | ```using EntityFrameworkCore.MemoryJoin``` 29 | 30 | 31 | Then 32 | 33 | // get context 34 | var context = CreateContext(); 35 | 36 | // define in-memory list 37 | var queryData = new [] { 38 | new { .StreetName = "Foo", .HouseNumber = 33 }, 39 | new { .StreetName = "Baz", .HouseNumber = 99 }, 40 | // can specify other objects here 41 | }; 42 | 43 | // get queryable representation, using thing like AsQueryable() will not work 44 | var queryable = context.FromLocalList(queryData); 45 | 46 | // Write as complex query as you want now. Data will be sent to server for performing query. I.e. 47 | var efQuery = from addr in context.Addresses 48 | join el in queryList on 49 | new { addr.StreetName, addr.HouseNumber } equals 50 | new { el.StreetName, el.HouseNumber } 51 | select new 52 | { 53 | addr.AddressId, 54 | addr.CreatedAt, 55 | addr.StreetName, 56 | addr.HouseNumber, 57 | el.Extra, 58 | el.Integer, 59 | el.Float 60 | }; 61 | 62 | // Query will be executed on DB server 63 | var = efQuery.ToList(); 64 | -------------------------------------------------------------------------------- /src/EntityFramework.MemoryJoin.TestRunner45/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 |
6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /src/EntityFramework.MemoryJoin.TestRunner45/DAL/Address.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.ComponentModel.DataAnnotations; 3 | using System.ComponentModel.DataAnnotations.Schema; 4 | 5 | namespace EntityFramework.MemoryJoin.TestRunner45.DAL 6 | { 7 | [Table("addresses", Schema = "public")] 8 | public class Address 9 | { 10 | [Column("address_id"), Key(), DatabaseGenerated(DatabaseGeneratedOption.Identity)] 11 | public int AddressId { get; set; } 12 | 13 | [Column("street_name"), Required()] 14 | public string StreetName { get; set; } 15 | 16 | [Column("house_number")] 17 | public int HouseNumber { get; set; } 18 | 19 | [Column("extra_house_number")] 20 | public int? ExtraHouseNumber { get; set; } 21 | 22 | [Column("postal_code"), Required()] 23 | public string PostalCode { get; set; } 24 | 25 | [Column("address_guid")] 26 | public Guid AddressGuid { get; set; } 27 | 28 | [Column("address_bool")] 29 | public bool AddressBool { get; set; } 30 | 31 | [Column("created_at"), DatabaseGenerated(DatabaseGeneratedOption.Computed)] 32 | public DateTime CreatedAt { get; set; } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/EntityFramework.MemoryJoin.TestRunner45/DAL/Price.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.ComponentModel.DataAnnotations; 3 | using System.ComponentModel.DataAnnotations.Schema; 4 | 5 | namespace EntityFramework.MemoryJoin.TestRunner45.DAL 6 | { 7 | [Table("price", Schema = "public")] 8 | public class Price 9 | { 10 | [Column("ticker"), Key()] 11 | public string Ticker { get; set; } 12 | 13 | [Column("traded_on")] 14 | public DateTime TradedOn { get; set; } 15 | 16 | [Column("source_id")] 17 | public int PriceSourceId { get; set; } 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/EntityFramework.MemoryJoin.TestRunner45/DAL/SampleContext.cs: -------------------------------------------------------------------------------- 1 | using System.Data.Entity; 2 | 3 | namespace EntityFramework.MemoryJoin.TestRunner45.DAL 4 | { 5 | public class SampleContext : DbContext 6 | { 7 | public DbSet
Addresses { get; set; } 8 | 9 | protected DbSet QueryData { get; set; } 10 | 11 | public SampleContext(string csName) : base(csName) { } 12 | 13 | protected override void OnModelCreating(DbModelBuilder modelBuilder) 14 | { 15 | base.OnModelCreating(modelBuilder); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/EntityFramework.MemoryJoin.TestRunner45/EntityFramework.MemoryJoin.TestRunner45.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {1AD8B2B8-6163-41B8-9D85-C3F8805E8580} 8 | Exe 9 | EntityFramework.MemoryJoin.TestRunner45 10 | EntityFramework.MemoryJoin.TestRunner45 11 | v4.5 12 | 512 13 | true 14 | 15 | 16 | 17 | AnyCPU 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | 26 | 27 | AnyCPU 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | 35 | 36 | 37 | ..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.dll 38 | 39 | 40 | ..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.SqlServer.dll 41 | 42 | 43 | ..\packages\EntityFramework6.Npgsql.3.1.1\lib\net45\EntityFramework6.Npgsql.dll 44 | 45 | 46 | ..\packages\Npgsql.3.2.7\lib\net45\Npgsql.dll 47 | 48 | 49 | 50 | 51 | 52 | ..\packages\System.Threading.Tasks.Extensions.4.3.0\lib\portable-net45+win8+wp8+wpa81\System.Threading.Tasks.Extensions.dll 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | {156a6d71-6341-4006-9701-7b222cbe8487} 74 | EntityFramework.MemoryJoin 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /src/EntityFramework.MemoryJoin.TestRunner45/Program.cs: -------------------------------------------------------------------------------- 1 | using EntityFramework.MemoryJoin.TestRunner45.DAL; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Data.Common; 5 | using System.Diagnostics; 6 | using System.Linq; 7 | 8 | namespace EntityFramework.MemoryJoin.TestRunner45 9 | { 10 | class Program 11 | { 12 | static void Main(string[] args) 13 | { 14 | 15 | #if NETSTANDARD 16 | DbProviderFactories.RegisterFactory("Npgsql", Npgsql.NpgsqlFactory.Instance); 17 | #endif 18 | 19 | var context = new SampleContext("DefaultConnection"); 20 | FillTestData(context); 21 | 22 | for (var count = 1000; count <= 10000; count += 1000) 23 | { 24 | var localList = GetTestAddressData(count). 25 | Select(x => new 26 | { 27 | x.StreetName, 28 | x.HouseNumber, 29 | Extra = "I'm from local!", 30 | Integer = ((long)int.MaxValue) + 20, 31 | Float = 321.0f, 32 | Date = DateTime.Now, 33 | Guid = Guid.NewGuid(), 34 | Bool = true 35 | }) 36 | .ToList(); 37 | 38 | var queryList = context.FromLocalList(localList, ValuesInjectionMethod.ViaSqlQueryBody); 39 | // var queryList2 = context.GetQueryable(localList); 40 | 41 | var efQuery = from addr in context.Addresses 42 | join el in queryList on 43 | new { addr.StreetName, addr.HouseNumber } equals 44 | new { el.StreetName, el.HouseNumber } 45 | select new 46 | { 47 | addr.AddressId, 48 | addr.CreatedAt, 49 | addr.StreetName, 50 | addr.HouseNumber, 51 | el.Extra, 52 | el.Integer, 53 | el.Float 54 | }; 55 | 56 | var sw = Stopwatch.StartNew(); 57 | var result = efQuery.ToList(); 58 | sw.Stop(); 59 | Console.WriteLine($"Success, requested: {count}, retrieved: {result.Count} elements in {sw.Elapsed}"); 60 | } 61 | Console.ReadLine(); 62 | } 63 | 64 | static List
GetTestAddressData(int count) 65 | { 66 | var streets = new[] { "First", "Second", "Third" }; 67 | var codes = new[] { "001001", "002002", "003003", "004004" }; 68 | var extraNumbers = new int?[] { null, 1, 2, 3, 5, 8, 13, 21, 34 }; 69 | 70 | var data = Enumerable.Range(0, count) 71 | .Select((x, i) => new Address() 72 | { 73 | StreetName = streets[i % streets.Length], 74 | HouseNumber = i + 1, 75 | PostalCode = codes[i % codes.Length], 76 | ExtraHouseNumber = extraNumbers[i % extraNumbers.Length], 77 | AddressGuid = Guid.NewGuid(), 78 | AddressBool = i % 1 == 1 79 | }).ToList(); 80 | 81 | return data; 82 | } 83 | 84 | static void FillTestData(SampleContext context) 85 | { 86 | if (context.Addresses.Any()) 87 | return; 88 | 89 | var data = GetTestAddressData(100000); 90 | 91 | context.Addresses.AddRange(data); 92 | context.SaveChanges(); 93 | } 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/EntityFramework.MemoryJoin.TestRunner45/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("EntityFramework.MemoryJoin.TestRunner45")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("EntityFramework.MemoryJoin.TestRunner45")] 13 | [assembly: AssemblyCopyright("Copyright © 2018")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("1ad8b2b8-6163-41b8-9d85-c3f8805e8580")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /src/EntityFramework.MemoryJoin.TestRunner45/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /src/EntityFramework.MemoryJoin.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.5.33516.290 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EntityFramework.MemoryJoin.TestRunner45", "EntityFramework.MemoryJoin.TestRunner45\EntityFramework.MemoryJoin.TestRunner45.csproj", "{1AD8B2B8-6163-41B8-9D85-C3F8805E8580}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EntityFramework.MemoryJoin", "EntityFramework.MemoryJoin\EntityFramework.MemoryJoin.csproj", "{156A6D71-6341-4006-9701-7B222CBE8487}" 9 | EndProject 10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityFrameworkCore.MemoryJoin", "EntityFrameworkCore.MemoryJoin\EntityFrameworkCore.MemoryJoin.csproj", "{B5BC8E1F-3D5A-4599-9DC2-FE109DA24A7E}" 11 | EndProject 12 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityFrameworkCore.MemoryJoin.TestRunnerCore", "EntityFrameworkCore.MemoryJoin.TestRunnerCore\EntityFrameworkCore.MemoryJoin.TestRunnerCore.csproj", "{26B3DE1A-5E61-4876-9ED0-21F40D149E6D}" 13 | EndProject 14 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "NuGet", "NuGet", "{B4EAFAEC-6EBC-4484-9519-FA879A7FD083}" 15 | ProjectSection(SolutionItems) = preProject 16 | CreatePackage.bat = CreatePackage.bat 17 | Package-ef6.nuspec = Package-ef6.nuspec 18 | Package.nuspec = Package.nuspec 19 | EndProjectSection 20 | EndProject 21 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityFrameworkCore.MemoryJoin.IntegrationTests", "EntityFrameworkCore.MemoryJoin.IntegrationTests\EntityFrameworkCore.MemoryJoin.IntegrationTests.csproj", "{3569F701-2F21-4880-BEDB-AC86D96EEC60}" 22 | EndProject 23 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityFrameworkCore.MemoryJoin.Ef6", "EntityFrameworkCore.MemoryJoin.Ef6\EntityFrameworkCore.MemoryJoin.Ef6.csproj", "{5500971F-0052-4453-84E2-85167F61E5D5}" 24 | EndProject 25 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityFrameworkCore.MemoryJoin.TestRunnerEf6", "EntityFrameworkCore.MemoryJoin.TestRunnerEf6\EntityFrameworkCore.MemoryJoin.TestRunnerEf6.csproj", "{93A24EF3-A719-46E2-8796-F3022B741699}" 26 | EndProject 27 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityFrameworkCore.MemoryJoin.TestRunnerNet5", "EntityFrameworkCore.MemoryJoin.TestRunnerNet5\EntityFrameworkCore.MemoryJoin.TestRunnerNet5.csproj", "{2B3A6A87-BE67-4FA1-ACB2-DB37FDCB3630}" 28 | EndProject 29 | Global 30 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 31 | Debug|Any CPU = Debug|Any CPU 32 | Release|Any CPU = Release|Any CPU 33 | EndGlobalSection 34 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 35 | {1AD8B2B8-6163-41B8-9D85-C3F8805E8580}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 36 | {1AD8B2B8-6163-41B8-9D85-C3F8805E8580}.Debug|Any CPU.Build.0 = Debug|Any CPU 37 | {1AD8B2B8-6163-41B8-9D85-C3F8805E8580}.Release|Any CPU.ActiveCfg = Release|Any CPU 38 | {1AD8B2B8-6163-41B8-9D85-C3F8805E8580}.Release|Any CPU.Build.0 = Release|Any CPU 39 | {156A6D71-6341-4006-9701-7B222CBE8487}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 40 | {156A6D71-6341-4006-9701-7B222CBE8487}.Debug|Any CPU.Build.0 = Debug|Any CPU 41 | {156A6D71-6341-4006-9701-7B222CBE8487}.Release|Any CPU.ActiveCfg = Release|Any CPU 42 | {156A6D71-6341-4006-9701-7B222CBE8487}.Release|Any CPU.Build.0 = Release|Any CPU 43 | {B5BC8E1F-3D5A-4599-9DC2-FE109DA24A7E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 44 | {B5BC8E1F-3D5A-4599-9DC2-FE109DA24A7E}.Debug|Any CPU.Build.0 = Debug|Any CPU 45 | {B5BC8E1F-3D5A-4599-9DC2-FE109DA24A7E}.Release|Any CPU.ActiveCfg = Release|Any CPU 46 | {B5BC8E1F-3D5A-4599-9DC2-FE109DA24A7E}.Release|Any CPU.Build.0 = Release|Any CPU 47 | {26B3DE1A-5E61-4876-9ED0-21F40D149E6D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 48 | {26B3DE1A-5E61-4876-9ED0-21F40D149E6D}.Debug|Any CPU.Build.0 = Debug|Any CPU 49 | {26B3DE1A-5E61-4876-9ED0-21F40D149E6D}.Release|Any CPU.ActiveCfg = Release|Any CPU 50 | {26B3DE1A-5E61-4876-9ED0-21F40D149E6D}.Release|Any CPU.Build.0 = Release|Any CPU 51 | {3569F701-2F21-4880-BEDB-AC86D96EEC60}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 52 | {3569F701-2F21-4880-BEDB-AC86D96EEC60}.Debug|Any CPU.Build.0 = Debug|Any CPU 53 | {3569F701-2F21-4880-BEDB-AC86D96EEC60}.Release|Any CPU.ActiveCfg = Release|Any CPU 54 | {3569F701-2F21-4880-BEDB-AC86D96EEC60}.Release|Any CPU.Build.0 = Release|Any CPU 55 | {5500971F-0052-4453-84E2-85167F61E5D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 56 | {5500971F-0052-4453-84E2-85167F61E5D5}.Debug|Any CPU.Build.0 = Debug|Any CPU 57 | {5500971F-0052-4453-84E2-85167F61E5D5}.Release|Any CPU.ActiveCfg = Release|Any CPU 58 | {5500971F-0052-4453-84E2-85167F61E5D5}.Release|Any CPU.Build.0 = Release|Any CPU 59 | {93A24EF3-A719-46E2-8796-F3022B741699}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 60 | {93A24EF3-A719-46E2-8796-F3022B741699}.Debug|Any CPU.Build.0 = Debug|Any CPU 61 | {93A24EF3-A719-46E2-8796-F3022B741699}.Release|Any CPU.ActiveCfg = Release|Any CPU 62 | {93A24EF3-A719-46E2-8796-F3022B741699}.Release|Any CPU.Build.0 = Release|Any CPU 63 | {2B3A6A87-BE67-4FA1-ACB2-DB37FDCB3630}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 64 | {2B3A6A87-BE67-4FA1-ACB2-DB37FDCB3630}.Debug|Any CPU.Build.0 = Debug|Any CPU 65 | {2B3A6A87-BE67-4FA1-ACB2-DB37FDCB3630}.Release|Any CPU.ActiveCfg = Release|Any CPU 66 | {2B3A6A87-BE67-4FA1-ACB2-DB37FDCB3630}.Release|Any CPU.Build.0 = Release|Any CPU 67 | EndGlobalSection 68 | GlobalSection(SolutionProperties) = preSolution 69 | HideSolutionNode = FALSE 70 | EndGlobalSection 71 | GlobalSection(ExtensibilityGlobals) = postSolution 72 | SolutionGuid = {8EAB61B9-F1A2-45FF-9DC5-F8832C1FFCE8} 73 | EndGlobalSection 74 | EndGlobal 75 | -------------------------------------------------------------------------------- /src/EntityFramework.MemoryJoin/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 |
6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/EntityFramework.MemoryJoin/EntityFramework.MemoryJoin.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {156A6D71-6341-4006-9701-7B222CBE8487} 8 | Library 9 | Properties 10 | EntityFramework.MemoryJoin 11 | EntityFramework.MemoryJoin 12 | v4.5 13 | 512 14 | 15 | 16 | 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | 25 | 26 | pdbonly 27 | true 28 | bin\Release\ 29 | TRACE 30 | prompt 31 | 4 32 | 33 | 34 | 35 | ..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.dll 36 | 37 | 38 | ..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.SqlServer.dll 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /src/EntityFramework.MemoryJoin/GlobalSuppressions.cs: -------------------------------------------------------------------------------- 1 |  2 | // This file is used by Code Analysis to maintain SuppressMessage 3 | // attributes that are applied to this project. 4 | // Project-level suppressions either have no target or are given 5 | // a specific target and scoped to a namespace, type, member, etc. 6 | 7 | [assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Security", "SG0026:Potential SQL injection with MsSQL Data Provider", Justification = "", Scope = "member", Target = "~M:EntityFramework.MemoryJoin.MemoryJoinerInterceptor.ModifyQuery(System.Data.Common.DbCommand,EntityFramework.MemoryJoin.Internal.InterceptionOptions)")] 8 | [assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Security", "SCS0026:Possible SQL injection", Justification = "", Scope = "member", Target = "~M:EntityFramework.MemoryJoin.MemoryJoinerInterceptor.ModifyQuery(System.Data.Common.DbCommand,EntityFramework.MemoryJoin.Internal.InterceptionOptions)")] 9 | 10 | -------------------------------------------------------------------------------- /src/EntityFramework.MemoryJoin/Internal/EFHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Data.Entity; 4 | using System.Data.Entity.Core.Mapping; 5 | using System.Data.Entity.Core.Metadata.Edm; 6 | using System.Data.Entity.Infrastructure; 7 | using System.Linq; 8 | using System.Reflection; 9 | 10 | namespace EntityFramework.MemoryJoin.Internal 11 | { 12 | /// 13 | /// Some internal helper methods for Npgsql 14 | /// 15 | internal static class EFHelper 16 | { 17 | 18 | internal static Dictionary ConvertFragmentToMapping( 19 | DbContext context, 20 | Type type, 21 | MappingFragment mappingFragment, 22 | EntityType entityType) 23 | { 24 | 25 | var innerList = mappingFragment 26 | .PropertyMappings 27 | .OfType() 28 | .ToDictionary(x => type.GetProperty(x.Property.Name, 29 | BindingFlags.NonPublic | BindingFlags.Public | 30 | BindingFlags.GetProperty | BindingFlags.Instance), 31 | x => x.Column.Name); 32 | 33 | return innerList; 34 | } 35 | 36 | internal static EntitySetBase GetEntitySet(DbContext context, Type type) 37 | { 38 | var metadata = ((IObjectContextAdapter)context).ObjectContext.MetadataWorkspace; 39 | var baseTypeName = type.BaseType.Name; 40 | var typeName = type.Name; 41 | 42 | var es = metadata 43 | .GetItemCollection(DataSpace.SSpace) 44 | .GetItems() 45 | .SelectMany(c => c.BaseEntitySets 46 | .Where(e => e.Name == typeName || e.Name == baseTypeName)) 47 | .FirstOrDefault(); 48 | 49 | return es; 50 | } 51 | 52 | internal static Dictionary GetColumnNames(DbContext context, Type type) 53 | { 54 | var metadata = ((IObjectContextAdapter)context).ObjectContext.MetadataWorkspace; 55 | var objectItemCollection = ((ObjectItemCollection)metadata.GetItemCollection(DataSpace.OSpace)); 56 | 57 | // Get the entity type from the model that maps to the CLR type 58 | var entityType = metadata.GetItems(DataSpace.OSpace) 59 | .Single(e => objectItemCollection.GetClrType(e) == type); 60 | 61 | var sets = metadata.GetItems(DataSpace.CSpace).Single().EntitySets; 62 | var entitySet = sets.SingleOrDefault(s => s.ElementType.Name == entityType.Name); 63 | 64 | var mappings = metadata.GetItems(DataSpace.CSSpace).Single().EntitySetMappings; 65 | 66 | if (entitySet != null) 67 | { 68 | var mapping = mappings.Single(s => s.EntitySet == entitySet); 69 | var typeMappings = mapping.EntityTypeMappings; 70 | var mappingFragment = (typeMappings.Count == 1 ? 71 | typeMappings.Single() : 72 | typeMappings.Single(x => x.EntityType == null)).Fragments.Single(); 73 | 74 | return ConvertFragmentToMapping(context, type, mappingFragment, entityType); 75 | } 76 | 77 | var partMapping = mappings 78 | .SelectMany(x => x.EntityTypeMappings) 79 | .Where(x => x.EntityType != null) 80 | .FirstOrDefault(x => x.EntityType.Name == type.Name); 81 | 82 | if (partMapping?.EntityType.BaseType == null) throw new NotSupportedException(); 83 | 84 | var baseEntityType = metadata.GetItems(DataSpace.OSpace) 85 | .Single(e => e.Name == partMapping.EntityType.BaseType.Name); 86 | var baseClrType = objectItemCollection.GetClrType(baseEntityType); 87 | 88 | var baseTypeMapping = GetColumnNames(context, baseClrType); 89 | var subTypeMapping = ConvertFragmentToMapping( 90 | context, type, partMapping.Fragments.Single(), entityType); 91 | 92 | var union = baseTypeMapping.Union(subTypeMapping) 93 | .ToDictionary(x => x.Key, x => x.Value); 94 | return union; 95 | } 96 | 97 | internal static string GetTableName(DbContext context, Type t) 98 | { 99 | var entityType = GetEntitySet(context, t); 100 | return entityType.Table; 101 | } 102 | 103 | internal static string GetKeyProperty(DbContext context, Type t) 104 | { 105 | var entityType = GetEntitySet(context, t); 106 | 107 | if (entityType == null) 108 | throw new InvalidOperationException( 109 | "QueryModelClass is not found in the context. Please check configuration"); 110 | 111 | var kps = entityType.ElementType.KeyProperties; 112 | if (kps.Count > 1) 113 | throw new NotSupportedException("Multiple column PK is not supported"); 114 | 115 | return kps.First().Name; 116 | } 117 | 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /src/EntityFramework.MemoryJoin/Internal/InterceptionOptions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace EntityFramework.MemoryJoin.Internal 5 | { 6 | internal class InterceptionOptions 7 | { 8 | public string DynamicTableName = "__gen_query_data__"; 9 | 10 | public string QueryTableName { get; set; } 11 | 12 | public List> Data { get; set; } 13 | 14 | public string[] ColumnNames { get; set; } 15 | 16 | public string KeyColumnName { get; set; } 17 | 18 | public Type ContextType { get; set; } 19 | 20 | public ValuesInjectionMethodInternal ValuesInjectMethod { get; set; } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/EntityFramework.MemoryJoin/Internal/KnownProvider.cs: -------------------------------------------------------------------------------- 1 | namespace EntityFramework.MemoryJoin.Internal 2 | { 3 | internal enum KnownProvider 4 | { 5 | Unknown = 0, 6 | 7 | Mssql = 1, 8 | 9 | PostgreSql = 2, 10 | 11 | Sqlite = 3, 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/EntityFramework.MemoryJoin/Internal/Mapping.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq.Expressions; 4 | 5 | namespace EntityFramework.MemoryJoin.Internal 6 | { 7 | internal class Mapping 8 | { 9 | public Dictionary> UserProperties { get; internal set; } 10 | 11 | public Expression OutExpression { get; internal set; } 12 | 13 | public string KeyColumnName { get; internal set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/EntityFramework.MemoryJoin/Internal/MappingHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Collections.Generic; 4 | using System.Reflection; 5 | using System.ComponentModel.DataAnnotations; 6 | using System.Linq.Expressions; 7 | using System.Text; 8 | using System.Collections; 9 | using System.Data.Common; 10 | using System.Collections.Concurrent; 11 | using System.Globalization; 12 | using System.Threading; 13 | #if EFCore 14 | using Microsoft.EntityFrameworkCore; 15 | #else 16 | using System.Data.Entity; 17 | #endif 18 | 19 | namespace EntityFramework.MemoryJoin.Internal 20 | { 21 | internal static class MappingHelper 22 | { 23 | private static readonly ConcurrentDictionary TypeToKnownProvider = 24 | new ConcurrentDictionary(); 25 | 26 | static int parametersSequence = 0; 27 | 28 | private static void ValidateAndExtendMapping(Dictionary mapping) 29 | { 30 | //if (!mapping.ContainsKey(typeof(long))) 31 | // throw new InvalidOperationException("Please include at least one property with `Long` type"); 32 | 33 | //if (!mapping.ContainsKey(typeof(double))) 34 | // throw new InvalidOperationException("Please include at least one property with `Double` type"); 35 | 36 | if (!mapping.ContainsKey(typeof(int))) 37 | mapping[typeof(int)] = mapping[typeof(long)]; 38 | 39 | if (!mapping.ContainsKey(typeof(Single))) 40 | mapping[typeof(Single)] = mapping[typeof(Double)]; 41 | 42 | if (!mapping.ContainsKey(typeof(decimal))) 43 | mapping[typeof(decimal)] = mapping[typeof(Double)]; 44 | 45 | if (!mapping.ContainsKey(typeof(bool))) 46 | mapping[typeof(bool)] = mapping[typeof(long)]; 47 | } 48 | 49 | internal static Dictionary GetPropertyMappings(Type queryClass) 50 | { 51 | var allowedMapping = queryClass. 52 | GetProperties(). 53 | Where(x => x.GetCustomAttribute() == null). 54 | GroupBy(x => Nullable.GetUnderlyingType(x.PropertyType) ?? x.PropertyType). 55 | ToDictionary(x => x.Key, x => x.ToArray()); 56 | ValidateAndExtendMapping(allowedMapping); 57 | 58 | return allowedMapping; 59 | } 60 | 61 | internal static Mapping GetEntityMapping( 62 | DbContext context, 63 | Type queryClass, 64 | Dictionary allowedPropertyMapping 65 | ) 66 | { 67 | var allowedProperties = new HashSet( 68 | allowedPropertyMapping.SelectMany(x => x.Value)); 69 | 70 | var inParam = Expression.Parameter(typeof(T), "x"); 71 | var inMappingPairs = new List>(); 72 | 73 | var outParam = Expression.Parameter(queryClass, "x"); 74 | var outMappingPairs = new List>(); 75 | 76 | var usedProperties = new Dictionary>(); 77 | var pkColumnName = EFHelper.GetKeyProperty(context, queryClass); 78 | if (pkColumnName == null) 79 | throw new NotSupportedException($"{queryClass.Name} should have PK set"); 80 | 81 | var columnNamesDict = EFHelper.GetColumnNames(context, queryClass); 82 | var members = typeof(T).GetProperties().Cast().Union(typeof(T).GetFields()); 83 | 84 | foreach (var member in members) 85 | { 86 | var memberType = member.MemberType == MemberTypes.Property ? 87 | ((PropertyInfo)member).PropertyType : 88 | ((FieldInfo)member).FieldType; 89 | 90 | var baseType = Nullable.GetUnderlyingType(memberType) ?? memberType; 91 | if (!allowedPropertyMapping.TryGetValue(baseType, out var allowedMappedProps)) 92 | throw new NotSupportedException("Not supported property type"); 93 | 94 | var mapProperty = allowedMappedProps.FirstOrDefault(x => allowedProperties.Contains(x)); 95 | if (mapProperty == null) 96 | throw new NotSupportedException($"Too complex object, need more properties of '{memberType.Name}' type in '{queryClass.Name}' class"); 97 | 98 | Expression inExp = Expression.MakeMemberAccess(inParam, member); 99 | if (mapProperty.PropertyType != memberType) 100 | inExp = Expression.Convert(inExp, mapProperty.PropertyType); 101 | 102 | inMappingPairs.Add(new Tuple(mapProperty, inExp)); 103 | 104 | Expression outExp = Expression.MakeMemberAccess(outParam, mapProperty); 105 | if (mapProperty.PropertyType != memberType) 106 | { 107 | if (memberType == typeof(bool)) 108 | { 109 | outExp = Expression.Equal(Expression.Convert(outExp, typeof(int)), Expression.Constant(1)); 110 | } 111 | else 112 | { 113 | outExp = Expression.Convert(outExp, memberType); 114 | } 115 | } 116 | 117 | outMappingPairs.Add(new Tuple(member, outExp)); 118 | 119 | allowedProperties.Remove(mapProperty); 120 | 121 | usedProperties.Add( 122 | columnNamesDict[mapProperty], 123 | (Func)(Expression.Lambda( 124 | Expression.Convert(inExp, typeof(object)), 125 | inParam 126 | ).Compile())); 127 | } 128 | 129 | var inCtor = queryClass.GetConstructor(new Type[] { }); 130 | var inNew = Expression.New(inCtor); 131 | var inBind = Expression.MemberInit(inNew, 132 | inMappingPairs.Select(x => Expression.Bind(x.Item1, x.Item2))); 133 | // var inExpression = Expression.Lambda(inBind, inParam); 134 | 135 | var outCtor = typeof(T).GetConstructors( 136 | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); 137 | var emptyCtor = outCtor.FirstOrDefault(x => x.GetParameters().Length == 0); 138 | Expression outExpression; 139 | 140 | if (emptyCtor != null) 141 | { 142 | var outNew = Expression.New(emptyCtor); 143 | var outBind = Expression.MemberInit(outNew, 144 | outMappingPairs.Select(x => Expression.Bind(x.Item1, x.Item2))); 145 | outExpression = Expression.Lambda(outBind, outParam); 146 | } 147 | else 148 | { 149 | var outNew = Expression.New( 150 | outCtor.First(), 151 | outMappingPairs.Select(x => x.Item2).ToArray(), 152 | outMappingPairs.Select(x => x.Item1).ToArray()); 153 | outExpression = Expression.Lambda(outNew, outParam); 154 | } 155 | 156 | return new Mapping() 157 | { 158 | UserProperties = usedProperties, 159 | OutExpression = outExpression, 160 | KeyColumnName = pkColumnName 161 | }; 162 | } 163 | 164 | internal static void ComposeTableSql( 165 | StringBuilder stringBuilder, 166 | InterceptionOptions options, 167 | DbCommand command, 168 | IList parameters) 169 | { 170 | var providerType = TypeToKnownProvider.GetOrAdd( 171 | options.ContextType, (t) => GetKnownProvider(command)); 172 | 173 | stringBuilder.Append("SELECT * FROM "); 174 | 175 | // SQLite doesn't support column aliasing in the FROM clause. Instead we have to do it 176 | // in a WITH clause. For example "SELECT * FROM (WITH tempTable(col1, col2, col3) AS (VALUES (1, 2, 3)) SELECT * FROM tempTable)". 177 | if (providerType == KnownProvider.Sqlite) 178 | { 179 | stringBuilder.Append("( WITH "); 180 | AppendColumnAliases(stringBuilder, options); 181 | stringBuilder.Append(" AS "); 182 | } 183 | 184 | if (options.Data.Any()) 185 | { 186 | AppendRowsAsValues(stringBuilder, options, command, parameters, providerType); 187 | } 188 | else 189 | { 190 | // if we have no data, we just generate 1 row consisting of null, because we need one row to define a table 191 | AppendNullRow(stringBuilder, options.ColumnNames.Length + 1); // + 1 since primary key column is not included. 192 | } 193 | 194 | if (providerType == KnownProvider.Sqlite) 195 | { 196 | stringBuilder.Append($" SELECT * FROM {options.DynamicTableName} )"); 197 | } 198 | else 199 | { 200 | stringBuilder.Append("AS "); 201 | AppendColumnAliases(stringBuilder, options); 202 | } 203 | 204 | // if we have no data, the in-memory table will consist of a null row. We don't want to return any result. As such, 205 | // append a condition which will always be false. 206 | if (!options.Data.Any()) 207 | { 208 | stringBuilder.Append(" WHERE 1=0"); 209 | } 210 | } 211 | 212 | private static void AppendRowsAsValues(StringBuilder sb, InterceptionOptions options, DbCommand command, IList parameters, KnownProvider providerType) 213 | { 214 | #if EFCore 215 | string paramPattern = EntityFrameworkCore.MemoryJoin.MemoryJoiner.ParametersPrefix; 216 | #else 217 | string paramPattern = EntityFramework.MemoryJoin.MemoryJoiner.ParametersPrefix; 218 | #endif 219 | 220 | Interlocked.Increment(ref parametersSequence); 221 | 222 | paramPattern = paramPattern + parametersSequence + "_"; 223 | 224 | var innerSb = new StringBuilder(20); 225 | var i = 0; 226 | var id = 1; 227 | 228 | sb.Append("( VALUES "); 229 | 230 | foreach (var el in options.Data) 231 | { 232 | sb.Append("("); 233 | // Let's append Id anyways, as per Issue #1 234 | 235 | sb.Append(id).Append(", "); 236 | foreach (var columnName in options.ColumnNames) 237 | { 238 | var value = el[columnName]; 239 | var stringValue = TryProcessParameterAsString(value, 240 | providerType, innerSb, options.ValuesInjectMethod); 241 | 242 | if (stringValue != null) 243 | { 244 | sb.Append(stringValue); 245 | } 246 | else 247 | { 248 | var paramName = $"{paramPattern}{i}"; 249 | var param = command.CreateParameter(); 250 | param.ParameterName = paramName; 251 | param.Value = value; 252 | parameters.Add(param); 253 | sb.Append(paramName); 254 | 255 | i++; 256 | } 257 | sb.Append(", "); 258 | } 259 | sb.Length -= ", ".Length; 260 | sb.Append("), "); 261 | id++; 262 | } 263 | 264 | sb.Length -= ", ".Length; 265 | sb.Append(")"); 266 | } 267 | 268 | private static void AppendNullRow(StringBuilder sb, int numberOfColumns) 269 | { 270 | sb.Append("(VALUES ("); 271 | for (var i = 0; i < numberOfColumns; i++) 272 | { 273 | sb.Append("NULL, "); 274 | } 275 | sb.Length -= 2; 276 | sb.Append("))"); 277 | } 278 | 279 | private static void AppendColumnAliases(StringBuilder sb, InterceptionOptions options) 280 | { 281 | 282 | sb.Append(options.DynamicTableName) 283 | .Append("(") 284 | .Append(options.KeyColumnName) 285 | .Append(", "); 286 | 287 | foreach (var cname in options.ColumnNames) 288 | { 289 | sb.Append(cname).Append(", "); 290 | } 291 | sb.Length -= 2; 292 | 293 | sb.Append(")"); 294 | } 295 | 296 | private static string TryProcessParameterAsString( 297 | object value, 298 | KnownProvider provider, 299 | StringBuilder sb, 300 | ValuesInjectionMethodInternal injectMethod) 301 | { 302 | // null is just 'NULL' 303 | if (value == null) 304 | return "NULL"; 305 | 306 | switch (injectMethod) 307 | { 308 | case ValuesInjectionMethodInternal.ViaParameters: 309 | return null; 310 | case ValuesInjectionMethodInternal.Auto: 311 | // Postgres has a huge limit for parameters, whereas MSSQL is just ... 2100 :( 312 | if (provider == KnownProvider.PostgreSql) 313 | return null; 314 | break; 315 | } 316 | 317 | // Try to inject parameters as text 318 | sb.Length = 0; 319 | switch (value) 320 | { 321 | case string strValue: 322 | sb.Append("'").Append(strValue.Replace("'", "''")).Append("'"); 323 | break; 324 | case float _: 325 | case double _: 326 | case decimal _: 327 | sb.Append(Convert.ToString(value, CultureInfo.InvariantCulture)); 328 | break; 329 | case int _: 330 | case long _: 331 | switch (provider) 332 | { 333 | case KnownProvider.Mssql: 334 | if (value is int) 335 | { 336 | sb.Append(Convert.ToString(value, CultureInfo.InvariantCulture)); 337 | } 338 | else 339 | { 340 | sb.Append("CAST(") 341 | .Append(Convert.ToString(value, CultureInfo.InvariantCulture)) 342 | .Append("AS BIGINT)"); 343 | } 344 | break; 345 | default: 346 | sb.Append(Convert.ToString(value, CultureInfo.InvariantCulture)); 347 | break; 348 | } 349 | 350 | break; 351 | case DateTime dateValue: 352 | switch (provider) 353 | { 354 | case KnownProvider.Mssql: 355 | sb.Append("CAST ('") 356 | .Append(dateValue.ToString("yyyy-MM-ddTHH:mm:ss.fff")) 357 | .Append("' AS DATETIME)"); 358 | break; 359 | case KnownProvider.PostgreSql: 360 | sb.Append("'") 361 | .Append(dateValue.ToString("yyyy-MM-ddTHH:mm:ss.fff")) 362 | .Append("'::date"); 363 | break; 364 | default: 365 | return null; 366 | } 367 | 368 | break; 369 | case Guid _: 370 | 371 | switch (provider) 372 | { 373 | case KnownProvider.PostgreSql: 374 | sb.Append("CAST('") 375 | .Append(Convert.ToString(value, CultureInfo.InvariantCulture)) 376 | .Append("' AS UUID)"); 377 | break; 378 | case KnownProvider.Mssql: 379 | sb.Append("CONVERT(uniqueidentifier, '") 380 | .Append(Convert.ToString(value, CultureInfo.InvariantCulture)) 381 | .Append("')"); 382 | break; 383 | default: 384 | sb.Append("'").Append(value).Append("'"); 385 | break; 386 | } 387 | 388 | break; 389 | default: 390 | return null; 391 | } 392 | 393 | return sb.ToString(); 394 | } 395 | 396 | static KnownProvider GetKnownProvider(DbCommand command) 397 | { 398 | var commandTypeName = command.GetType().Name; 399 | 400 | if (commandTypeName.StartsWith("Npgsql")) 401 | return KnownProvider.PostgreSql; 402 | if (commandTypeName.StartsWith("SqlCommand")) 403 | return KnownProvider.Mssql; 404 | if (commandTypeName.Contains("SqliteCommand")) 405 | return KnownProvider.Sqlite; 406 | 407 | return KnownProvider.Unknown; 408 | } 409 | 410 | } 411 | } 412 | -------------------------------------------------------------------------------- /src/EntityFramework.MemoryJoin/Internal/ValuesInjectionMethodInternal.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace EntityFramework.MemoryJoin.Internal 8 | { 9 | /// 10 | /// States a method of parameters injection into SQL query 11 | /// 12 | public enum ValuesInjectionMethodInternal 13 | { 14 | /// 15 | /// Method will be picked automatically 16 | /// 17 | Auto = 0, 18 | 19 | /// 20 | /// ALL values will go as parameters 21 | /// Pros: values will go safely 22 | /// Cons: there is a limit on parametes count in MS SQL, max is 2100 parameters 23 | /// 24 | ViaParameters = 1, 25 | 26 | /// 27 | /// Values will be injected as a text to SQL query 28 | /// 29 | ViaSqlQueryBody = 2 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/EntityFramework.MemoryJoin/MemoryJoiner.cs: -------------------------------------------------------------------------------- 1 | using EntityFramework.MemoryJoin.Internal; 2 | using System; 3 | using System.Collections.Concurrent; 4 | using System.Collections.Generic; 5 | using System.Data.Entity; 6 | using System.Data.Entity.Infrastructure.Interception; 7 | using System.Linq; 8 | using System.Reflection; 9 | 10 | namespace EntityFramework.MemoryJoin 11 | { 12 | 13 | public static class MemoryJoiner 14 | { 15 | /// 16 | /// Parameter names prefix which is used in SQL expression 17 | /// 18 | public static string ParametersPrefix { get; set; } = "@__gen_q_p_"; 19 | 20 | static ConcurrentDictionary> allowedMappingDict = 21 | new ConcurrentDictionary>(); 22 | static MethodInfo selectMethod = typeof(Queryable).GetMethods() 23 | .Where(x => x.Name == "Select") 24 | .OrderBy(x => x.GetParameters().Length) 25 | .First(); 26 | 27 | static MethodInfo takeMethod = typeof(Queryable) 28 | .GetTypeInfo() 29 | .GetMethods() 30 | .Where(x => x.Name == "Take") 31 | .First(); 32 | 33 | 34 | 35 | static MemoryJoiner() 36 | { 37 | DbInterception.Add(new MemoryJoinerInterceptor()); 38 | } 39 | 40 | /// 41 | /// Returns queryable wrapper for data 42 | /// 43 | public static IQueryable FromLocalList(this DbContext context, IList data) 44 | { 45 | return FromLocalList(context, data, typeof(QueryModelClass)); 46 | } 47 | 48 | /// 49 | /// Returns queryable wrapper for data 50 | /// 51 | public static IQueryable FromLocalList(this DbContext context, IList data, ValuesInjectionMethod method) 52 | { 53 | return FromLocalList(context, data, typeof(QueryModelClass), method); 54 | } 55 | 56 | /// 57 | /// Returns queryable wrapper for data 58 | /// 59 | public static IQueryable FromLocalList(this DbContext context, IList data) 60 | { 61 | return FromLocalList(context, data, typeof(TQueryModel)); 62 | } 63 | 64 | /// 65 | /// Returns queryable wrapper for data 66 | /// 67 | public static IQueryable FromLocalList(this DbContext context, IList data, Type queryClass) 68 | { 69 | return FromLocalList(context, data, queryClass, ValuesInjectionMethod.Auto); 70 | } 71 | 72 | /// 73 | /// Returns queryable wrapper for data 74 | /// 75 | /// 76 | /// 77 | /// 78 | public static IQueryable FromLocalList(this DbContext context, IList data, Type queryClass, ValuesInjectionMethod method) 79 | { 80 | if (MemoryJoinerInterceptor.IsInterceptionEnabled( 81 | new[] { context }, out InterceptionOptions opts)) 82 | { 83 | throw new InvalidOperationException( 84 | "Only one data set can be applied to single DbContext before actuall DB request is done"); 85 | } 86 | 87 | var propMapping = allowedMappingDict.GetOrAdd(queryClass, MappingHelper.GetPropertyMappings); 88 | var entityMapping = MappingHelper.GetEntityMapping(context, queryClass, propMapping); 89 | var baseQuerySet = context.Set(queryClass); 90 | 91 | if (data.Any()) 92 | { 93 | PrepareInjection(entityMapping, data, context, queryClass, method); 94 | 95 | var middleResult = selectMethod.MakeGenericMethod(queryClass, typeof(T)) 96 | .Invoke(null, new object[] { baseQuerySet, entityMapping.OutExpression }); 97 | 98 | var querySet = (IQueryable)middleResult; 99 | return querySet; 100 | } 101 | else 102 | { 103 | var emptyQueryable = takeMethod.MakeGenericMethod(queryClass).Invoke(null, new object[] { baseQuerySet, 0 }); 104 | 105 | var middleResult = selectMethod.MakeGenericMethod(queryClass, typeof(T)) 106 | .Invoke(null, new object[] { emptyQueryable, entityMapping.OutExpression }); 107 | 108 | var querySet = (IQueryable)middleResult; 109 | return querySet; 110 | } 111 | } 112 | 113 | static void PrepareInjection( 114 | Mapping mapping, 115 | IList data, 116 | DbContext context, 117 | Type queryClass, 118 | ValuesInjectionMethod method) 119 | { 120 | var opts = new InterceptionOptions 121 | { 122 | QueryTableName = EFHelper.GetTableName(context, queryClass), 123 | ColumnNames = mapping.UserProperties.Keys.ToArray(), 124 | Data = data 125 | .Select(x => mapping.UserProperties.ToDictionary(y => y.Key, y => y.Value(x))) 126 | .ToList(), 127 | ContextType = context.GetType(), 128 | ValuesInjectMethod = (ValuesInjectionMethodInternal)method, 129 | KeyColumnName = mapping.KeyColumnName 130 | }; 131 | 132 | MemoryJoinerInterceptor.SetInterception(context, opts); 133 | } 134 | 135 | } 136 | } 137 | -------------------------------------------------------------------------------- /src/EntityFramework.MemoryJoin/MemoryJoinerInterceptor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using EntityFramework.MemoryJoin.Internal; 3 | using System.Collections.Concurrent; 4 | using System.Collections.Generic; 5 | using System.Data.Common; 6 | using System.Data.Entity; 7 | using System.Data.Entity.Infrastructure.Interception; 8 | using System.Text; 9 | 10 | namespace EntityFramework.MemoryJoin 11 | { 12 | internal class MemoryJoinerInterceptor : IDbCommandInterceptor 13 | { 14 | private static readonly ConcurrentDictionary InterceptionOptions = 15 | new ConcurrentDictionary(); 16 | 17 | internal static void SetInterception(DbContext context, InterceptionOptions options) 18 | { 19 | InterceptionOptions[context] = options; 20 | } 21 | 22 | internal static bool IsInterceptionEnabled(IEnumerable contexts, out InterceptionOptions options) 23 | { 24 | options = null; 25 | using (var enumerator = contexts.GetEnumerator()) 26 | { 27 | if (!enumerator.MoveNext()) return false; 28 | 29 | var firstOne = enumerator.Current; 30 | var result = firstOne != null && 31 | InterceptionOptions.TryGetValue(firstOne, out options) && 32 | !enumerator.MoveNext(); 33 | if (result) 34 | InterceptionOptions.TryRemove(firstOne, out options); 35 | 36 | return result; 37 | } 38 | } 39 | 40 | public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext interceptionContext) 41 | { 42 | } 43 | 44 | public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext interceptionContext) 45 | { 46 | if (IsInterceptionEnabled(interceptionContext.DbContexts, out var opts)) 47 | ModifyQuery(command, opts); 48 | } 49 | 50 | public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext interceptionContext) 51 | { 52 | } 53 | 54 | public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext interceptionContext) 55 | { 56 | if (IsInterceptionEnabled(interceptionContext.DbContexts, out var opts)) 57 | ModifyQuery(command, opts); 58 | } 59 | 60 | public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext interceptionContext) 61 | { 62 | } 63 | 64 | public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext interceptionContext) 65 | { 66 | if (IsInterceptionEnabled(interceptionContext.DbContexts, out var opts)) 67 | ModifyQuery(command, opts); 68 | } 69 | 70 | private static void ModifyQuery(DbCommand command, InterceptionOptions opts) 71 | { 72 | var tableNamePosition = command.CommandText.IndexOf(opts.QueryTableName, StringComparison.Ordinal); 73 | if (tableNamePosition < 0) 74 | return; 75 | 76 | var nextSpace = command.CommandText.IndexOf(' ', tableNamePosition); 77 | var prevSpace = command.CommandText.LastIndexOf(' ', tableNamePosition); 78 | var tableFullName = command.CommandText.Substring(prevSpace + 1, nextSpace - prevSpace - 1); 79 | 80 | command.CommandText = command.CommandText.Replace(tableFullName, opts.DynamicTableName); 81 | 82 | var sb = new StringBuilder(100); 83 | sb.Append("WITH ").Append(opts.DynamicTableName).Append(" AS (").AppendLine(); 84 | MappingHelper.ComposeTableSql( 85 | sb, opts, 86 | command, 87 | command.Parameters); 88 | 89 | sb.AppendLine(); 90 | sb.AppendLine(")"); 91 | sb.Append(command.CommandText); 92 | 93 | command.CommandText = sb.ToString(); 94 | } 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/EntityFramework.MemoryJoin/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("EntityFramework.MemoryJoin")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("EntityFramework.MemoryJoin")] 13 | [assembly: AssemblyCopyright("Copyright © Anton Shkuratov 2018")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("156a6d71-6341-4006-9701-7b222cbe8487")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("0.7.7.0")] 36 | [assembly: AssemblyFileVersion("0.7.7.0")] 37 | -------------------------------------------------------------------------------- /src/EntityFramework.MemoryJoin/QueryModelClass.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.ComponentModel.DataAnnotations; 3 | using System.ComponentModel.DataAnnotations.Schema; 4 | 5 | namespace EntityFramework.MemoryJoin 6 | { 7 | [Table("__stub_query_data", Schema = "__stub")] 8 | public class QueryModelClass 9 | { 10 | [Column("id"), Key(), DatabaseGenerated(DatabaseGeneratedOption.Identity)] 11 | public int Id { get; set; } 12 | 13 | [Column("long1")] 14 | public long? Long1 { get; set; } 15 | 16 | [Column("long2")] 17 | public long? Long2 { get; set; } 18 | 19 | [Column("long3")] 20 | public long? Long3 { get; set; } 21 | 22 | [Column("double1")] 23 | public Double? Double1 { get; set; } 24 | 25 | [Column("double2")] 26 | public Double? Double2 { get; set; } 27 | 28 | [Column("double3")] 29 | public Double? Double3 { get; set; } 30 | 31 | [Column("string1")] 32 | public string String1 { get; set; } 33 | 34 | [Column("string2")] 35 | public string String2 { get; set; } 36 | 37 | [Column("string3")] 38 | public string String3 { get; set; } 39 | 40 | [Column("date1")] 41 | public DateTime? Date1 { get; set; } 42 | 43 | [Column("date2")] 44 | public DateTime? Date2 { get; set; } 45 | 46 | [Column("date3")] 47 | public DateTime? Date3 { get; set; } 48 | 49 | [Column("guid1")] 50 | public Guid? Guid1 { get; set; } 51 | 52 | [Column("guid2")] 53 | public Guid? Guid2 { get; set; } 54 | 55 | [Column("guid3")] 56 | public Guid? Guid3 { get; set; } 57 | 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/EntityFramework.MemoryJoin/ValuesInjectionMethod.cs: -------------------------------------------------------------------------------- 1 | using EntityFramework.MemoryJoin.Internal; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace EntityFramework.MemoryJoin 9 | { 10 | /// 11 | /// States a method of parameters injection into SQL query 12 | /// 13 | public enum ValuesInjectionMethod 14 | { 15 | /// 16 | /// Method will be picked automatically 17 | /// 18 | Auto = ValuesInjectionMethodInternal.Auto, 19 | 20 | /// 21 | /// ALL values will go as parameters 22 | /// Pros: values will go safely 23 | /// Cons: there is a limit on parametes count in MS SQL, max is 2100 parameters 24 | /// 25 | ViaParameters = ValuesInjectionMethodInternal.ViaParameters, 26 | 27 | /// 28 | /// Values will be injected as a text to SQL query 29 | /// 30 | ViaSqlQueryBody = ValuesInjectionMethodInternal.ViaSqlQueryBody 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/EntityFramework.MemoryJoin/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /src/EntityFrameworkCore.MemoryJoin.Ef6/EntityFrameworkCore.MemoryJoin.Ef6.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netstandard2.1 5 | Copyright © Anton Shkuratov 2018 6 | 7 | Anton Shkuratov 8 | EntityFramework.MemoryJoin 9 | 0.7.7 10 | 0.7.7.0 11 | 0.7.7.0 12 | false 13 | EntityFrameworkCore.MemoryJoin 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /src/EntityFrameworkCore.MemoryJoin.IntegrationTests/EntityFrameworkCore.MemoryJoin.IntegrationTests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netcoreapp3.1;net6.0 5 | 6 | false 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /src/EntityFrameworkCore.MemoryJoin.IntegrationTests/SqliteTests.cs: -------------------------------------------------------------------------------- 1 | using EntityFrameworkCore.MemoryJoin.IntegrationTests.TestModels; 2 | using EntityFrameworkCore.MemoryJoin.IntegrationTests.Utils; 3 | using Microsoft.Data.Sqlite; 4 | using Microsoft.EntityFrameworkCore; 5 | using Microsoft.VisualStudio.TestTools.UnitTesting; 6 | using System.Collections.Generic; 7 | using System.Linq; 8 | using System.Threading.Tasks; 9 | 10 | namespace EntityFrameworkCore.MemoryJoin.IntegrationTests 11 | { 12 | [TestClass] 13 | public class SqliteTests 14 | { 15 | public TestContext TestContext { get; set; } 16 | 17 | [TestMethod] 18 | public async Task FromLocalList_ShouldReturnEmptyCollection_WhenJoinSourcesAreBothEmpty() 19 | { 20 | using (var context = CreateTestDbContext(nameof(FromLocalList_ShouldReturnEmptyCollection_WhenJoinSourcesAreBothEmpty))) 21 | { 22 | // test with both an empty in-memory collection and an empty real table 23 | var emptyCollection = new List(0); 24 | 25 | var inMemoryEntities = context.FromLocalList(emptyCollection); 26 | 27 | var joinedQueryable = context.Entities.Join( 28 | inMemoryEntities, 29 | inner => inner.Id, 30 | outer => outer.Id, 31 | (inner, outer) => new { Id = inner.Id, FromRealTable = inner.TestString, FromInMemoryTable = outer.Prop1 }); 32 | 33 | var result = await joinedQueryable.ToListAsync(); 34 | Assert.AreEqual(0, result.Count); 35 | } 36 | } 37 | 38 | [TestMethod] 39 | public async Task FromLocalList_ShouldReturnEmptyCollection_WhenInMemoryEntitiesIsEmpty() 40 | { 41 | using (var context = CreateTestDbContext(nameof(FromLocalList_ShouldReturnEmptyCollection_WhenInMemoryEntitiesIsEmpty))) 42 | { 43 | // test with an empty in-memory collection 44 | var emptyCollection = new List(0); 45 | var inMemoryEntities = context.FromLocalList(emptyCollection); 46 | 47 | // fill up some values in the real table 48 | context.AddRange( 49 | new TestEntity { Id = "1", TestInt = 1234, TestString = "abc" }, 50 | new TestEntity { Id = "2", TestInt = 5678, TestString = "def" }); 51 | await context.SaveChangesAsync(); 52 | 53 | var joinedQueryable = context.Entities.Join( 54 | inMemoryEntities, 55 | inner => inner.Id, 56 | outer => outer.Id, 57 | (inner, outer) => new { Id = inner.Id, FromRealTable = inner.TestString, FromInMemoryTable = outer.Prop1 }); 58 | 59 | var result = await joinedQueryable.ToListAsync(); 60 | Assert.AreEqual(0, result.Count); 61 | } 62 | } 63 | 64 | [TestMethod] 65 | public async Task FromLocalList_ShouldReturnJoinedCollection_WhenSourcesContainAnIntersection() 66 | { 67 | using (var context = CreateTestDbContext(nameof(FromLocalList_ShouldReturnEmptyCollection_WhenInMemoryEntitiesIsEmpty))) 68 | { 69 | // fill up the in-memory table with 1 match 70 | var queryData = new List { new TestInMemoryEntity { Id = "1", Prop1 = 999 } }; 71 | var inMemoryEntities = context.FromLocalList(queryData); 72 | 73 | // fill up some values in the real table 74 | context.AddRange( 75 | new TestEntity { Id = "1", TestInt = 1234, TestString = "abc" }, 76 | new TestEntity { Id = "2", TestInt = 5678, TestString = "def" }); 77 | await context.SaveChangesAsync(); 78 | 79 | var joinedQueryable = context.Entities.Join( 80 | inMemoryEntities, 81 | inner => inner.Id, 82 | outer => outer.Id, 83 | (inner, outer) => new { Id = inner.Id, FromRealTable = inner.TestString, FromInMemoryTable = outer.Prop1 }); 84 | 85 | var result = await joinedQueryable.ToListAsync(); 86 | Assert.AreEqual(1, result.Count); 87 | Assert.AreEqual(new { Id = "1", FromRealTable = "abc", FromInMemoryTable = 999 }, result.First()); 88 | } 89 | } 90 | 91 | private TestDbContext CreateTestDbContext(string databaseName) 92 | { 93 | var builder = new DbContextOptionsBuilder(); 94 | 95 | // Create an in-memory DB for test purposes. 96 | var connection = new SqliteConnection("Data Source=:memory:;"); 97 | connection.Open(); 98 | builder = builder.UseSqlite(connection).EnableSensitiveDataLogging(true); 99 | 100 | if (TestContext != default) 101 | { 102 | builder.UseLoggerFactory(new TestLoggerFactory(() => TestContext)); 103 | } 104 | 105 | var context = new TestDbContext(builder.Options); 106 | context.Database.EnsureCreated(); 107 | 108 | return context; 109 | } 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /src/EntityFrameworkCore.MemoryJoin.IntegrationTests/TestModels/TestDbContext.cs: -------------------------------------------------------------------------------- 1 | namespace EntityFrameworkCore.MemoryJoin.IntegrationTests.TestModels 2 | { 3 | using Microsoft.EntityFrameworkCore; 4 | 5 | public class TestDbContext : DbContext 6 | { 7 | public TestDbContext(DbContextOptions options) 8 | : base(options) 9 | { 10 | } 11 | 12 | public DbSet Entities { get; set; } 13 | 14 | protected DbSet QueryData { get; set; } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/EntityFrameworkCore.MemoryJoin.IntegrationTests/TestModels/TestEntity.cs: -------------------------------------------------------------------------------- 1 | namespace EntityFrameworkCore.MemoryJoin.IntegrationTests.TestModels 2 | { 3 | public class TestEntity 4 | { 5 | public string Id { get; set; } 6 | 7 | public int TestInt { get; set; } 8 | 9 | public string TestString { get; set; } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/EntityFrameworkCore.MemoryJoin.IntegrationTests/TestModels/TestInMemoryEntity.cs: -------------------------------------------------------------------------------- 1 | namespace EntityFrameworkCore.MemoryJoin.IntegrationTests.TestModels 2 | { 3 | public class TestInMemoryEntity 4 | { 5 | public string Id { get; set; } 6 | 7 | public int Prop1 { get; set; } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/EntityFrameworkCore.MemoryJoin.IntegrationTests/Utils/TestLogger.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.Extensions.Logging; 3 | using Microsoft.VisualStudio.TestTools.UnitTesting; 4 | 5 | namespace EntityFrameworkCore.MemoryJoin.IntegrationTests.Utils 6 | { 7 | /// 8 | /// An implementation of that will attach the logs to the test case output. 9 | /// 10 | internal class TestLogger : ILogger 11 | { 12 | private readonly TestContext context; 13 | 14 | public TestLogger(TestContext context) => this.context = context; 15 | 16 | /// 17 | public IDisposable BeginScope(TState state) => StubScope.Instance; 18 | 19 | /// 20 | public bool IsEnabled(LogLevel logLevel) => true; 21 | 22 | /// 23 | public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) 24 | { 25 | try 26 | { 27 | this.context.WriteLine($"[{logLevel}]: {formatter(state, exception)} (EventId: {eventId})"); 28 | } 29 | catch (InvalidOperationException) 30 | { 31 | // InvalidOperationExceptions are thrown when no test case is running (e.g. during Dispose()). 32 | // As such, if the service is logging something during disposal, we can't log it associated to 33 | // the test case. 34 | } 35 | } 36 | 37 | private class StubScope : IDisposable 38 | { 39 | public static readonly StubScope Instance = new StubScope(); 40 | 41 | public void Dispose() 42 | { 43 | // Method intentionally left empty. 44 | } 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/EntityFrameworkCore.MemoryJoin.IntegrationTests/Utils/TestLoggerFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.Extensions.Logging; 3 | using Microsoft.VisualStudio.TestTools.UnitTesting; 4 | 5 | namespace EntityFrameworkCore.MemoryJoin.IntegrationTests.Utils 6 | { 7 | /// 8 | /// An implementation of that will produce s. 9 | /// 10 | internal class TestLoggerFactory : ILoggerFactory 11 | { 12 | private readonly Func testContextFactory; 13 | 14 | /// 15 | /// Initializes a new instance of the class. 16 | /// 17 | /// 18 | /// A factory for producing s. When calling upon this factory, 19 | /// it is expected to product a for the active test case. 20 | /// 21 | public TestLoggerFactory(Func testContextFactory) => this.testContextFactory = testContextFactory; 22 | 23 | /// 24 | public void AddProvider(ILoggerProvider provider) 25 | { 26 | // Ignore any providers passed to this factory. 27 | } 28 | 29 | /// 30 | public ILogger CreateLogger(string categoryName) => new TestLogger(this.testContextFactory()); 31 | 32 | /// 33 | public void Dispose() 34 | { 35 | // Intentionally left blank as there are no resources to dispose 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/EntityFrameworkCore.MemoryJoin.TestRunnerCore/DAL/Address.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.ComponentModel.DataAnnotations; 3 | using System.ComponentModel.DataAnnotations.Schema; 4 | 5 | namespace EntityFrameworkCore.MemoryJoin.TestRunnerCore.DAL 6 | { 7 | [Table("addresses")] 8 | public class Address 9 | { 10 | [Column("address_id"), Key(), DatabaseGenerated(DatabaseGeneratedOption.Identity)] 11 | public int AddressId { get; set; } 12 | 13 | [Column("street_name"), Required()] 14 | public string StreetName; 15 | 16 | [Column("house_number")] 17 | public int HouseNumber { get; set; } 18 | 19 | [Column("extra_house_number")] 20 | public int? ExtraHouseNumber { get; set; } 21 | 22 | [Column("postal_code"), Required()] 23 | public string PostalCode { get; set; } 24 | 25 | [Column("created_at")] 26 | public DateTime CreatedAt { get; set; } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/EntityFrameworkCore.MemoryJoin.TestRunnerCore/DAL/SampleContext.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | 3 | namespace EntityFrameworkCore.MemoryJoin.TestRunnerCore.DAL 4 | { 5 | public class SampleContext : DbContext 6 | { 7 | public DbSet
Addresses { get; set; } 8 | 9 | protected DbSet QueryData { get; set; } 10 | 11 | public SampleContext(DbContextOptions options) : base(options) { } 12 | 13 | protected override void OnModelCreating(ModelBuilder modelBuilder) 14 | { 15 | modelBuilder.Entity
().Property("StreetName"); 16 | 17 | modelBuilder.Entity().ToTable("betterName", "schema"); 18 | 19 | base.OnModelCreating(modelBuilder); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/EntityFrameworkCore.MemoryJoin.TestRunnerCore/EntityFrameworkCore.MemoryJoin.TestRunnerCore.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | netcoreapp3.0;net6.0;net7.0 6 | 7 | 8 | 9 | TRACE;EFCore 10 | 11 | 12 | 13 | TRACE;EFCore 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /src/EntityFrameworkCore.MemoryJoin.TestRunnerCore/Program.cs: -------------------------------------------------------------------------------- 1 | using EntityFrameworkCore.MemoryJoin.TestRunnerCore.DAL; 2 | using Microsoft.EntityFrameworkCore; 3 | using Microsoft.Extensions.Logging; 4 | using Microsoft.Extensions.Logging.Console; 5 | using System; 6 | using System.Collections.Generic; 7 | using System.Diagnostics; 8 | using System.Linq; 9 | 10 | namespace EntityFrameworkCore.MemoryJoin.TestRunner45 11 | { 12 | class Program 13 | { 14 | static void Main(string[] args) 15 | { 16 | var optionsBuilder = new DbContextOptionsBuilder(); 17 | 18 | optionsBuilder.AddInterceptors(new MemoryJoinerInterceptor()); 19 | optionsBuilder.UseNpgsql("server=localhost;user id=postgres;password=qwerty;database=copy"); 20 | //optionsBuilder.UseSqlServer("Data Source=localhost;Initial Catalog=copy;Integrated Security=True;Pooling=False"); 21 | 22 | //optionsBuilder.UseLoggerFactory( 23 | // new LoggerFactory(new[] { new ConsoleLoggerProvider((_, __) => true, true) })); 24 | 25 | var context = new SampleContext(optionsBuilder.Options); 26 | context.Database.EnsureCreated(); 27 | 28 | FillTestData(context); 29 | 30 | for (var count = 0; count <= 1000; count += 100) 31 | { 32 | var localList = GetTestAddressData(count). 33 | Select(x => new 34 | { 35 | x.StreetName, 36 | x.HouseNumber, 37 | DateTime = DateTime.Now, 38 | Extra = "I'm from \n ' \" local!", 39 | Integer = ((long)int.MaxValue) + 20, 40 | Float = 321.0f, 41 | Date = DateTime.Now, 42 | Bool = true, 43 | Guid = Guid.NewGuid() 44 | }) 45 | .ToList(); 46 | 47 | var queryList = context.FromLocalList(localList, ValuesInjectionMethod.Auto); 48 | 49 | var efQuery = from addr in context.Addresses 50 | join el in queryList on 51 | new { addr.StreetName, addr.HouseNumber } equals 52 | new { el.StreetName, el.HouseNumber } 53 | select new 54 | { 55 | addr.AddressId, 56 | addr.CreatedAt, 57 | addr.StreetName, 58 | addr.HouseNumber, 59 | el.Extra, 60 | el.Integer, 61 | el.Float, 62 | el.DateTime, 63 | el.Guid, 64 | el.Bool 65 | }; 66 | 67 | var sw = Stopwatch.StartNew(); 68 | var result = efQuery.ToList(); 69 | sw.Stop(); 70 | Console.WriteLine($"Success, requested: {count}, retrieved: {result.Count} elements in {sw.Elapsed}"); 71 | } 72 | 73 | // test double queries 74 | var query1 = context.FromLocalList(GetTestAddressData(10)); 75 | var query2 = context.FromLocalList(GetTestAddressData(10)); 76 | 77 | var efQuery2 = from addr in context.Addresses 78 | join el1 in query1 on addr.StreetName equals el1.StreetName 79 | join el2 in query2 on addr.HouseNumber equals el2.HouseNumber 80 | select new 81 | { 82 | addr.AddressId, 83 | el1.StreetName, 84 | el2.HouseNumber 85 | }; 86 | 87 | var dblRes = efQuery2.ToArray(); 88 | Console.WriteLine("Double query executed..."); 89 | 90 | Console.WriteLine("Going to use interception mode"); 91 | MemoryJoiner.MemoryJoinerMode = MemoryJoinerMode.UsingInterception; 92 | 93 | for (var count = 0; count <= 1000; count += 100) 94 | { 95 | var localList = GetTestAddressData(count). 96 | Select(x => new 97 | { 98 | x.StreetName, 99 | x.HouseNumber, 100 | DateTime = DateTime.Now, 101 | Extra = "I'm from \n ' \" local!", 102 | Integer = ((long)int.MaxValue) + 20, 103 | Float = 321.0f, 104 | Date = DateTime.Now, 105 | Bool = true, 106 | Guid = Guid.NewGuid() 107 | }) 108 | .ToList(); 109 | 110 | var queryList = context.FromLocalList(localList, ValuesInjectionMethod.Auto); 111 | 112 | var efQuery = from addr in context.Addresses 113 | join el in queryList on 114 | new { addr.StreetName, addr.HouseNumber } equals 115 | new { el.StreetName, el.HouseNumber } 116 | select new 117 | { 118 | addr.AddressId, 119 | addr.CreatedAt, 120 | addr.StreetName, 121 | addr.HouseNumber, 122 | el.Extra, 123 | el.Integer, 124 | el.Float, 125 | el.DateTime, 126 | el.Guid, 127 | el.Bool 128 | }; 129 | 130 | var sw = Stopwatch.StartNew(); 131 | var result = efQuery.ToList(); 132 | sw.Stop(); 133 | Console.WriteLine($"Success, requested: {count}, retrieved: {result.Count} elements in {sw.Elapsed}"); 134 | } 135 | 136 | Console.ReadLine(); 137 | } 138 | 139 | 140 | 141 | static List
GetTestAddressData(int count) 142 | { 143 | var streets = new[] { "First", "Second", "Third" }; 144 | var codes = new[] { "001001", "002002", "003003", "004004" }; 145 | var extraNumbers = new int?[] { null, 1, 2, 3, 5, 8, 13, 21, 34 }; 146 | 147 | var data = Enumerable.Range(0, count) 148 | .Select((x, i) => new Address() 149 | { 150 | StreetName = streets[i % streets.Length], 151 | HouseNumber = i + 1, 152 | PostalCode = codes[i % codes.Length], 153 | ExtraHouseNumber = extraNumbers[i % extraNumbers.Length], 154 | CreatedAt = DateTime.Now 155 | }).ToList(); 156 | 157 | return data; 158 | } 159 | 160 | static void FillTestData(SampleContext context) 161 | { 162 | if (context.Addresses.Any()) 163 | return; 164 | 165 | var data = GetTestAddressData(10000); 166 | 167 | context.Addresses.AddRange(data); 168 | context.SaveChanges(); 169 | } 170 | 171 | } 172 | } 173 | -------------------------------------------------------------------------------- /src/EntityFrameworkCore.MemoryJoin.TestRunnerEf6/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 |
6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /src/EntityFrameworkCore.MemoryJoin.TestRunnerEf6/EntityFrameworkCore.MemoryJoin.TestRunnerEf6.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | netcoreapp3.0 6 | 7 | 8 | 9 | TRACE;NETSTANDARD 10 | 11 | 12 | 13 | TRACE;NETSTANDARD 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /src/EntityFrameworkCore.MemoryJoin.TestRunnerNet5/EntityFrameworkCore.MemoryJoin.TestRunnerNet5.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net5.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /src/EntityFrameworkCore.MemoryJoin/EntityFrameworkCore.MemoryJoin.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netstandard2.1;net6.0;net7.0 5 | Copyright © Anton Shkuratov 2018 6 | 7 | Anton Shkuratov 8 | EntityFramework.MemoryJoin 9 | 0.9.0 10 | 0.9.0.0 11 | 0.9.0.0 12 | false 13 | 14 | 15 | 16 | TRACE;EFCore 17 | 18 | 19 | 20 | TRACE;EFCore 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /src/EntityFrameworkCore.MemoryJoin/Internal/EFHelper.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Reflection; 6 | 7 | namespace EntityFramework.MemoryJoin.Internal 8 | { 9 | /// 10 | /// Some internal helper methods for EF 11 | /// 12 | internal static class EFHelper 13 | { 14 | internal static Dictionary GetColumnNames(DbContext context, Type type) 15 | { 16 | var metadata = context.Model; 17 | var entityType = metadata.GetEntityTypes().Single(x => x.ClrType == type); 18 | 19 | 20 | var innerList = entityType 21 | .GetProperties() 22 | .Where(x => x.PropertyInfo != null) 23 | .ToDictionary(x => x.PropertyInfo, x => x.GetColumnName()); 24 | 25 | return innerList; 26 | } 27 | 28 | internal static string GetTableName(DbContext context, Type t) 29 | { 30 | var relational = context.Model.FindEntityType(t); 31 | return relational.GetTableName(); 32 | } 33 | 34 | internal static string GetKeyProperty(DbContext context, Type t) 35 | { 36 | var entityType = context.Model.FindEntityType(t); 37 | if (entityType == null) 38 | throw new InvalidOperationException( 39 | "QueryModelClass is not found in the context. Please check configuration"); 40 | 41 | var kps = entityType.FindPrimaryKey().Properties; 42 | if (kps.Count > 1) 43 | throw new NotSupportedException("Multiple column PK is not supported"); 44 | 45 | return kps.First().GetColumnName(); 46 | } 47 | 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/EntityFrameworkCore.MemoryJoin/MemoryJoiner.cs: -------------------------------------------------------------------------------- 1 | using EntityFramework.MemoryJoin.Internal; 2 | using Microsoft.EntityFrameworkCore; 3 | using System; 4 | using System.Collections.Concurrent; 5 | using System.Collections.Generic; 6 | using System.Data.Common; 7 | using System.Linq; 8 | using System.Reflection; 9 | using System.Text; 10 | 11 | namespace EntityFrameworkCore.MemoryJoin 12 | { 13 | 14 | public static class MemoryJoiner 15 | { 16 | /// 17 | /// Parameter names prefix which is used in SQL expression 18 | /// 19 | public static string ParametersPrefix { get; set; } = "@__gen_q_p_"; 20 | 21 | static MethodInfo selectMethod = typeof(Queryable) 22 | .GetTypeInfo() 23 | .GetMethods() 24 | .Where(x => x.Name == "Select") 25 | .OrderBy(x => x.GetParameters().Length) 26 | .First(); 27 | 28 | static MethodInfo setMethod = typeof(DbContext).GetMethod("Set", new Type[] { }); 29 | 30 | static Type rawSqlStringType = typeof(RelationalQueryableExtensions).Assembly.GetType("Microsoft.EntityFrameworkCore.RawSqlString"); 31 | 32 | static MethodInfo fromSqlMethod = typeof(RelationalQueryableExtensions) 33 | .GetTypeInfo() 34 | .GetMethods() 35 | .Where(x => x.Name == "FromSql") 36 | .OrderByDescending(x => x.GetParameters().Length) 37 | .FirstOrDefault(); 38 | 39 | static MethodInfo fromSqlRawMethod = typeof(RelationalQueryableExtensions) 40 | .GetTypeInfo() 41 | .GetMethods() 42 | .Where(x => x.Name == "FromSqlRaw") 43 | .OrderByDescending(x => x.GetParameters().Length) 44 | .FirstOrDefault(); 45 | 46 | static ConcurrentDictionary> allowedMappingDict = 47 | new ConcurrentDictionary>(); 48 | 49 | public static MemoryJoinerMode MemoryJoinerMode { get; set; } 50 | 51 | /// 52 | /// Returns queryable wrapper for data 53 | /// 54 | public static IQueryable FromLocalList(this DbContext context, IList data) 55 | { 56 | return FromLocalList(context, data, typeof(QueryModelClass)); 57 | } 58 | 59 | /// 60 | /// Returns queryable wrapper for data 61 | /// 62 | public static IQueryable FromLocalList(this DbContext context, IList data, ValuesInjectionMethod method) 63 | { 64 | return FromLocalList(context, data, typeof(QueryModelClass), method); 65 | } 66 | 67 | /// 68 | /// Returns queryable wrapper for data 69 | /// 70 | public static IQueryable FromLocalList(this DbContext context, IList data) 71 | { 72 | return FromLocalList(context, data, typeof(TQueryModel)); 73 | } 74 | 75 | /// 76 | /// Returns queryable wrapper for data 77 | /// 78 | public static IQueryable FromLocalList(this DbContext context, IList data, Type queryClass) 79 | { 80 | return FromLocalList(context, data, queryClass, ValuesInjectionMethod.Auto); 81 | } 82 | 83 | public static IQueryable FromLocalList(this DbContext context, IList data, Type queryClass, ValuesInjectionMethod method) 84 | { 85 | if (MemoryJoinerMode == MemoryJoinerMode.UsingInterception) 86 | return MemoryJoinerViaInterception.FromLocalList(context, data, queryClass, method); 87 | 88 | var sb = new StringBuilder(100); 89 | 90 | var propMapping = allowedMappingDict.GetOrAdd(queryClass, (t) => MappingHelper.GetPropertyMappings(t)); 91 | var entityMapping = MappingHelper.GetEntityMapping(context, queryClass, propMapping); 92 | 93 | var opts = new InterceptionOptions 94 | { 95 | QueryTableName = EFHelper.GetTableName(context, queryClass), 96 | ColumnNames = entityMapping.UserProperties.Keys.ToArray(), 97 | Data = data 98 | .Select(x => entityMapping.UserProperties.ToDictionary(y => y.Key, y => y.Value(x))) 99 | .ToList(), 100 | ContextType = context.GetType(), 101 | ValuesInjectMethod = (ValuesInjectionMethodInternal)method, 102 | KeyColumnName = entityMapping.KeyColumnName 103 | }; 104 | 105 | var connection = context.Database.GetDbConnection(); 106 | using (var command = connection.CreateCommand()) 107 | { 108 | var parameters = new List(); 109 | MappingHelper.ComposeTableSql( 110 | sb, 111 | opts, 112 | command, 113 | parameters); 114 | 115 | var set = setMethod.MakeGenericMethod(queryClass) 116 | .Invoke(context, new object[] { }); 117 | 118 | if (fromSqlMethod != null) 119 | { 120 | #if NET6_0 121 | var rawSqlString = Activator.CreateInstance(rawSqlStringType, new object[] { sb.ToString() }); 122 | 123 | var fromSql = fromSqlMethod 124 | .MakeGenericMethod(queryClass) 125 | .Invoke(null, new object[] { set, rawSqlString, parameters.ToArray() }); 126 | 127 | var middleResult = selectMethod.MakeGenericMethod(queryClass, typeof(T)) 128 | .Invoke(null, new object[] { fromSql, entityMapping.OutExpression }); 129 | 130 | var querySet = (IQueryable)middleResult; 131 | return querySet; 132 | #elif NET7_0_OR_GREATER 133 | var fromSql = fromSqlRawMethod 134 | .MakeGenericMethod(queryClass) 135 | .Invoke(null, new object[] { set, sb.ToString(), parameters.ToArray() }); 136 | 137 | var middleResult = selectMethod.MakeGenericMethod(queryClass, typeof(T)) 138 | .Invoke(null, new object[] { fromSql, entityMapping.OutExpression }); 139 | 140 | var querySet = (IQueryable)middleResult; 141 | return querySet; 142 | #else 143 | throw new NotImplementedException(); 144 | #endif 145 | } 146 | else 147 | { 148 | // .Net 5 case 149 | var fromSql = fromSqlRawMethod 150 | .MakeGenericMethod(queryClass) 151 | .Invoke(null, new object[] { set, sb.ToString(), parameters.ToArray() }); 152 | 153 | var middleResult = selectMethod.MakeGenericMethod(queryClass, typeof(T)) 154 | .Invoke(null, new object[] { fromSql, entityMapping.OutExpression }); 155 | 156 | var querySet = (IQueryable)middleResult; 157 | return querySet; 158 | } 159 | } 160 | } 161 | 162 | } 163 | } 164 | -------------------------------------------------------------------------------- /src/EntityFrameworkCore.MemoryJoin/QueryModelClass.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.ComponentModel.DataAnnotations; 3 | using System.ComponentModel.DataAnnotations.Schema; 4 | 5 | namespace EntityFrameworkCore.MemoryJoin 6 | { 7 | [Table("__stub_query_data", Schema = "__stub")] 8 | public class QueryModelClass 9 | { 10 | [Column("id"), Key(), DatabaseGenerated(DatabaseGeneratedOption.Identity)] 11 | public int Id { get; set; } 12 | 13 | 14 | [Column("long1")] 15 | public long? Long1 { get; set; } 16 | 17 | [Column("long2")] 18 | public long? Long2 { get; set; } 19 | 20 | [Column("long3")] 21 | public long? Long3 { get; set; } 22 | 23 | [Column("double1")] 24 | public Double? Double1 { get; set; } 25 | 26 | [Column("double2")] 27 | public Double? Double2 { get; set; } 28 | 29 | [Column("double3")] 30 | public Double? Double3 { get; set; } 31 | 32 | [Column("string1")] 33 | public string String1 { get; set; } 34 | 35 | [Column("string2")] 36 | public string String2 { get; set; } 37 | 38 | [Column("string3")] 39 | public string String3 { get; set; } 40 | 41 | [Column("date1")] 42 | public DateTime? Date1 { get; set; } 43 | 44 | [Column("date2")] 45 | public DateTime? Date2 { get; set; } 46 | 47 | [Column("date3")] 48 | public DateTime? Date3 { get; set; } 49 | 50 | [Column("guid1")] 51 | public Guid? Guid1 { get; set; } 52 | 53 | [Column("guid2")] 54 | public Guid? Guid2 { get; set; } 55 | 56 | [Column("guid3")] 57 | public Guid? Guid3 { get; set; } 58 | 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/EntityFrameworkCore.MemoryJoin/ValuesInjectionMethod.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace EntityFrameworkCore.MemoryJoin 8 | { 9 | /// 10 | /// States a method of parameters injection into SQL query 11 | /// 12 | public enum ValuesInjectionMethod 13 | { 14 | /// 15 | /// Method will be picked automatically 16 | /// 17 | Auto = 0, 18 | 19 | /// 20 | /// ALL values will go as parameters 21 | /// Pros: values will go safely 22 | /// Cons: there is a limit on parametes count in MS SQL, max is 2100 parameters 23 | /// 24 | ViaParameters = 1, 25 | 26 | /// 27 | /// Values will be injected as a text to SQL query 28 | /// 29 | ViaSqlQueryBody = 2 30 | } 31 | } 32 | --------------------------------------------------------------------------------