Skip to content

Commit a2c71d6

Browse files
committedMar 18, 2025
New sample
1 parent 4286731 commit a2c71d6

File tree

10 files changed

+219
-0
lines changed

10 files changed

+219
-0
lines changed
 

‎DynamicQuery/BlogPost.cs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
public record BlogPost
2+
{
3+
public required int Id { get; set; }
4+
public required string Title { get; set; }
5+
public required string Content { get; set; }
6+
public required string Author { get; set; }
7+
public required DateTime Created { get; set; }
8+
public required DateTime Updated { get; set; }
9+
public required List<string> Tags { get; set; }
10+
}

‎DynamicQuery/BloggingContext.cs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
public class BloggingContext : DbContext
4+
{
5+
public DbSet<BlogPost> BlogPosts { get; set; } = default!;
6+
7+
protected override void OnConfiguring(DbContextOptionsBuilder options)
8+
=> options.UseSqlite("DataSource=myshareddb;mode=memory;cache=shared");
9+
10+
protected override void OnModelCreating(ModelBuilder modelBuilder)
11+
{
12+
modelBuilder.ApplyConfiguration(new BlogPostTypeConfiguration());
13+
}
14+
}

‎DynamicQuery/DynamicQuery.csproj

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<LangVersion>preview</LangVersion>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.3" />
13+
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.6.0.2" />
14+
</ItemGroup>
15+
16+
</Project>

‎DynamicQuery/DynamicQuery.sln

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DynamicQuery", "DynamicQuery.csproj", "{CDAC80B9-217D-411D-97D5-94193BF1AB70}"
4+
EndProject
5+
Global
6+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7+
Debug|Any CPU = Debug|Any CPU
8+
Release|Any CPU = Release|Any CPU
9+
EndGlobalSection
10+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
11+
{CDAC80B9-217D-411D-97D5-94193BF1AB70}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
12+
{CDAC80B9-217D-411D-97D5-94193BF1AB70}.Debug|Any CPU.Build.0 = Debug|Any CPU
13+
{CDAC80B9-217D-411D-97D5-94193BF1AB70}.Release|Any CPU.ActiveCfg = Release|Any CPU
14+
{CDAC80B9-217D-411D-97D5-94193BF1AB70}.Release|Any CPU.Build.0 = Release|Any CPU
15+
EndGlobalSection
16+
EndGlobal

‎DynamicQuery/Program.cs

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System.Linq.Expressions;
2+
using System.Linq.Dynamic.Core;
3+
using Microsoft.EntityFrameworkCore;
4+
using Microsoft.EntityFrameworkCore.Metadata.Builders;
5+
6+
using var dbContext = new BloggingContext();
7+
dbContext.Database.EnsureCreated();
8+
9+
Console.WriteLine("What column would you like to return?");
10+
var column = Console.ReadLine();
11+
12+
var columnSelector = GenerateColumnSelector(column).Compile();
13+
var query = dbContext.BlogPosts.Select(s => new ReturnColumn
14+
{
15+
Id = s.Id,
16+
Column = columnSelector.Invoke(s)
17+
});
18+
19+
var queryString = query.ToQueryString();
20+
Console.WriteLine(queryString);
21+
22+
var queryWithDynamic = dbContext
23+
.BlogPosts
24+
.Select<ReturnColumn>($"new(Id, {column} AS Column)");
25+
26+
var queryStringWithDynamic = queryWithDynamic.ToQueryString();
27+
Console.WriteLine(queryStringWithDynamic);
28+
29+
Expression<Func<BlogPost, string>> GenerateColumnSelector(string? column)
30+
{
31+
return column switch
32+
{
33+
"Title" => s => s.Title,
34+
"Content" => s => s.Content,
35+
"Author" => s => s.Author,
36+
_ => throw new ArgumentException("Invalid column name")
37+
};
38+
}
39+
40+
public class ReturnColumn
41+
{
42+
public int Id { get; set; }
43+
public string Column { get; set; }
44+
}
45+
46+
public class BlogPostTypeConfiguration : IEntityTypeConfiguration<BlogPost>
47+
{
48+
public void Configure(EntityTypeBuilder<BlogPost> builder)
49+
{
50+
builder.HasKey(x => x.Id);
51+
builder.Property(x => x.Title).HasMaxLength(2048).IsRequired();
52+
builder.Property(x => x.Content).IsRequired();
53+
builder.Property(x => x.Author).HasMaxLength(2048).IsRequired();
54+
builder.Property(x => x.Created).IsRequired();
55+
builder.Property(x => x.Updated).IsRequired();
56+
builder.Property(x => x.Tags).IsRequired();
57+
}
58+
}

‎DynamicQuery/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Solving Problems you wouldn't have if you wouldn't use Entity Framework
2+
3+
Long title - short intro? Well - I recently came across my own stupidity and wanted to display that to the world. Basically, how to dynamically selecting a column with Entity Framework.
4+
5+
Found [here](https://steven-giesel.com/blogPost/1b432667-3b2d-4c41-8557-b4c3a3c554d9/solving-problems-you-wouldnt-have-if-you-wouldnt-use-entity-framework)

‎PerformanceNet10/PerformanceNet10.sln

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PerformanceNet10", "PerformanceNet10\PerformanceNet10.csproj", "{D8EAFDC8-5C18-4384-8FF4-51A1455C4725}"
4+
EndProject
5+
Global
6+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7+
Debug|Any CPU = Debug|Any CPU
8+
Release|Any CPU = Release|Any CPU
9+
EndGlobalSection
10+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
11+
{D8EAFDC8-5C18-4384-8FF4-51A1455C4725}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
12+
{D8EAFDC8-5C18-4384-8FF4-51A1455C4725}.Debug|Any CPU.Build.0 = Debug|Any CPU
13+
{D8EAFDC8-5C18-4384-8FF4-51A1455C4725}.Release|Any CPU.ActiveCfg = Release|Any CPU
14+
{D8EAFDC8-5C18-4384-8FF4-51A1455C4725}.Release|Any CPU.Build.0 = Release|Any CPU
15+
EndGlobalSection
16+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>net9.0;net10.0</TargetFrameworks>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="BenchmarkDotNet" Version="0.14.1-nightly.20250107.205" />
12+
</ItemGroup>
13+
14+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+

2+
using BenchmarkDotNet.Attributes;
3+
using BenchmarkDotNet.Jobs;
4+
using BenchmarkDotNet.Running;
5+
6+
BenchmarkSwitcher.FromAssembly(typeof(VirtualizationAndInlineBenchmarks).Assembly).Run();
7+
8+
[SimpleJob(RuntimeMoniker.Net90, baseline: true)]
9+
[SimpleJob(RuntimeMoniker.Net10_0)]
10+
[MemoryDiagnoser]
11+
public class VirtualizationAndInlineBenchmarks
12+
{
13+
private static readonly List<int> Numbers = Enumerable.Range(0, 10000).Select(s => Random.Shared.Next()).ToList();
14+
15+
// See: https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-10/runtime
16+
[Benchmark]
17+
public int Sum()
18+
{
19+
var sum = 0;
20+
IEnumerable<int> temp = Numbers;
21+
foreach (var t in temp)
22+
sum += t;
23+
24+
return sum;
25+
}
26+
27+
// See: https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-10/runtime
28+
[Benchmark]
29+
public int StackallocOfArrays()
30+
{
31+
int[] numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
32+
var sum = 0;
33+
34+
for (var i = 0; i < numbers.Length; i++)
35+
sum += numbers[i];
36+
37+
return sum;
38+
}
39+
}
40+
41+
[SimpleJob(RuntimeMoniker.Net90, baseline: true)]
42+
[SimpleJob(RuntimeMoniker.Net10_0)]
43+
[MemoryDiagnoser]
44+
public class CommonListOperationBenchmarks
45+
{
46+
[Benchmark]
47+
public List<int> ListAdd10000()
48+
{
49+
var list = new List<int>();
50+
for (var i = 0; i < 10_000; i++)
51+
{
52+
list.Add(i);
53+
}
54+
55+
return list;
56+
}
57+
58+
[Benchmark]
59+
public List<int> ListAdd10000PreAlloc()
60+
{
61+
var list = new List<int>(10_000);
62+
for (var i = 0; i < 10_000; i++)
63+
{
64+
list.Add(i);
65+
}
66+
67+
return list;
68+
}
69+
}

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Contains all of my examples from various blog posts. You can find a comprehensiv
44

55
| BlogPost | Publish Date |
66
| -------------------------------------------------------------------------------------------------------------- | ------------ |
7+
| [Solving Problems you wouldn't have if you wouldn't use Entity Framework](DynamicQuery/) | 24.04.2025 |
78
| [Tailwind v4 with Blazor - It just got easier](BlazorTailwind4/) | 03.03.2025 |
89
| [ToArray(Async) vs ToList(Async) in Entity Framework 8](BenchmarkToArrayToListEF/) | 28.10.2024 |
910
| [How to test HttpClient inside API Tests](FakeHttpClient/) | 02.09.2024 |

0 commit comments

Comments
 (0)
Please sign in to comment.