-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathProgram.cs
58 lines (49 loc) · 1.71 KB
/
Program.cs
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System.Linq.Expressions;
using System.Linq.Dynamic.Core;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using var dbContext = new BloggingContext();
dbContext.Database.EnsureCreated();
Console.WriteLine("What column would you like to return?");
var column = Console.ReadLine();
var columnSelector = GenerateColumnSelector(column).Compile();
var query = dbContext.BlogPosts.Select(s => new ReturnColumn
{
Id = s.Id,
Column = columnSelector.Invoke(s)
});
var queryString = query.ToQueryString();
Console.WriteLine(queryString);
var queryWithDynamic = dbContext
.BlogPosts
.Select<ReturnColumn>($"new(Id, {column} AS Column)");
var queryStringWithDynamic = queryWithDynamic.ToQueryString();
Console.WriteLine(queryStringWithDynamic);
Expression<Func<BlogPost, string>> GenerateColumnSelector(string? column)
{
return column switch
{
"Title" => s => s.Title,
"Content" => s => s.Content,
"Author" => s => s.Author,
_ => throw new ArgumentException("Invalid column name")
};
}
public class ReturnColumn
{
public int Id { get; set; }
public string Column { get; set; }
}
public class BlogPostTypeConfiguration : IEntityTypeConfiguration<BlogPost>
{
public void Configure(EntityTypeBuilder<BlogPost> builder)
{
builder.HasKey(x => x.Id);
builder.Property(x => x.Title).HasMaxLength(2048).IsRequired();
builder.Property(x => x.Content).IsRequired();
builder.Property(x => x.Author).HasMaxLength(2048).IsRequired();
builder.Property(x => x.Created).IsRequired();
builder.Property(x => x.Updated).IsRequired();
builder.Property(x => x.Tags).IsRequired();
}
}