1
- using EntityFrameworkCore . Projectables ;
2
- using EntityFrameworkCore . Projectables . Extensions ;
3
- using Microsoft . Data . Sqlite ;
4
- using Microsoft . EntityFrameworkCore ;
5
- using Microsoft . Extensions . Caching . Memory ;
6
- using Microsoft . Extensions . DependencyInjection ;
7
- using Microsoft . Extensions . Logging ;
8
- using System ;
9
- using System . Collections ;
1
+ using System ;
10
2
using System . Collections . Generic ;
11
3
using System . ComponentModel . DataAnnotations . Schema ;
12
- using System . Diagnostics ;
13
4
using System . Linq ;
5
+ using EntityFrameworkCore . Projectables ;
6
+ using Microsoft . Data . Sqlite ;
7
+ using Microsoft . EntityFrameworkCore ;
8
+ using Microsoft . Extensions . DependencyInjection ;
14
9
15
10
namespace BasicSample
16
11
{
@@ -22,12 +17,13 @@ public class User
22
17
23
18
public ICollection < Order > Orders { get ; set ; }
24
19
25
- [ Projectable ]
26
- public string FullName
27
- => FirstName + " " + LastName ;
20
+ [ Projectable ( UseMemberBody = nameof ( _FullName ) ) ]
21
+ public string FullName { get ; set ; }
22
+ private string _FullName => FirstName + " " + LastName ;
28
23
29
- [ Projectable ]
30
- public double TotalSpent => Orders . Sum ( x => x . PriceSum ) ;
24
+ [ Projectable ( UseMemberBody = nameof ( _TotalSpent ) ) ]
25
+ public double TotalSpent { get ; set ; }
26
+ private double _TotalSpent => Orders . Sum ( x => x . PriceSum ) ;
31
27
32
28
[ Projectable ]
33
29
public Order MostValuableOrder
@@ -86,7 +82,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
86
82
87
83
class Program
88
84
{
89
- static void Main ( string [ ] args )
85
+ public static void Main ( string [ ] args )
90
86
{
91
87
using var dbConnection = new SqliteConnection ( "Filename=:memory:" ) ;
92
88
dbConnection . Open ( ) ;
@@ -95,6 +91,8 @@ static void Main(string[] args)
95
91
. AddDbContext < ApplicationDbContext > ( ( provider , options ) => {
96
92
options
97
93
. UseSqlite ( dbConnection )
94
+ // .LogTo(Console.WriteLine)
95
+ . EnableSensitiveDataLogging ( )
98
96
. UseProjectables ( ) ;
99
97
} )
100
98
. BuildServiceProvider ( ) ;
@@ -105,9 +103,9 @@ static void Main(string[] args)
105
103
var product1 = new Product { Name = "Red pen" , Price = 1.5 } ;
106
104
var product2 = new Product { Name = "Blue pen" , Price = 2.1 } ;
107
105
108
- var user = new User {
109
- FirstName = "Jon" ,
110
- LastName = "Doe" ,
106
+ var user = new User {
107
+ FirstName = "Jon" ,
108
+ LastName = "Doe" ,
111
109
Orders = new List < Order > {
112
110
new Order {
113
111
Items = new List < OrderItem > {
@@ -130,6 +128,42 @@ static void Main(string[] args)
130
128
dbContext . SaveChanges ( ) ;
131
129
132
130
// What did our user spent in total
131
+
132
+ {
133
+ foreach ( var u in dbContext . Users )
134
+ {
135
+ Console . WriteLine ( $ "User name: { u . FullName } ") ;
136
+ }
137
+
138
+ foreach ( var u in dbContext . Users . ToList ( ) )
139
+ {
140
+ Console . WriteLine ( $ "User name: { u . FullName } ") ;
141
+ }
142
+
143
+ foreach ( var u in dbContext . Users . OrderBy ( x => x . FullName ) )
144
+ {
145
+ Console . WriteLine ( $ "User name: { u . FullName } ") ;
146
+ }
147
+ }
148
+
149
+ {
150
+ foreach ( var u in dbContext . Users . Where ( x => x . TotalSpent >= 1 ) )
151
+ {
152
+ Console . WriteLine ( $ "User name: { u . FullName } ") ;
153
+ }
154
+ }
155
+
156
+ {
157
+ var result = dbContext . Users . FirstOrDefault ( ) ;
158
+ Console . WriteLine ( $ "Our first user { result . FullName } has spent { result . TotalSpent } ") ;
159
+
160
+ result = dbContext . Users . FirstOrDefault ( x => x . TotalSpent > 1 ) ;
161
+ Console . WriteLine ( $ "Our first user { result . FullName } has spent { result . TotalSpent } ") ;
162
+
163
+ var spent = dbContext . Users . Sum ( x => x . TotalSpent ) ;
164
+ Console . WriteLine ( $ "Our users combined spent: { spent } ") ;
165
+ }
166
+
133
167
{
134
168
var query = dbContext . Users
135
169
. Select ( x => new {
0 commit comments