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 ;
102using System . Collections . Generic ;
113using System . ComponentModel . DataAnnotations . Schema ;
12- using System . Diagnostics ;
134using System . Linq ;
5+ using EntityFrameworkCore . Projectables ;
6+ using Microsoft . Data . Sqlite ;
7+ using Microsoft . EntityFrameworkCore ;
8+ using Microsoft . Extensions . DependencyInjection ;
149
1510namespace BasicSample
1611{
@@ -22,12 +17,13 @@ public class User
2217
2318 public ICollection < Order > Orders { get ; set ; }
2419
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 ;
2823
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 ) ;
3127
3228 [ Projectable ]
3329 public Order MostValuableOrder
@@ -86,7 +82,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
8682
8783 class Program
8884 {
89- static void Main ( string [ ] args )
85+ public static void Main ( string [ ] args )
9086 {
9187 using var dbConnection = new SqliteConnection ( "Filename=:memory:" ) ;
9288 dbConnection . Open ( ) ;
@@ -95,6 +91,8 @@ static void Main(string[] args)
9591 . AddDbContext < ApplicationDbContext > ( ( provider , options ) => {
9692 options
9793 . UseSqlite ( dbConnection )
94+ // .LogTo(Console.WriteLine)
95+ . EnableSensitiveDataLogging ( )
9896 . UseProjectables ( ) ;
9997 } )
10098 . BuildServiceProvider ( ) ;
@@ -105,9 +103,9 @@ static void Main(string[] args)
105103 var product1 = new Product { Name = "Red pen" , Price = 1.5 } ;
106104 var product2 = new Product { Name = "Blue pen" , Price = 2.1 } ;
107105
108- var user = new User {
109- FirstName = "Jon" ,
110- LastName = "Doe" ,
106+ var user = new User {
107+ FirstName = "Jon" ,
108+ LastName = "Doe" ,
111109 Orders = new List < Order > {
112110 new Order {
113111 Items = new List < OrderItem > {
@@ -130,6 +128,42 @@ static void Main(string[] args)
130128 dbContext . SaveChanges ( ) ;
131129
132130 // 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+
133167 {
134168 var query = dbContext . Users
135169 . Select ( x => new {
0 commit comments