mirror of
https://github.com/zoriya/EntityFrameworkCore.Projectables.git
synced 2025-12-18 19:45:12 +00:00
Cleaned up readme sample
This commit is contained in:
@@ -12,6 +12,7 @@ namespace ReadmeSample.Entities
|
|||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
public int UserId { get; set; }
|
public int UserId { get; set; }
|
||||||
public DateTime CreatedDate { get; set; }
|
public DateTime CreatedDate { get; set; }
|
||||||
|
public DateTime? FulfilledDate { get; set; }
|
||||||
|
|
||||||
public decimal TaxRate { get; set; }
|
public decimal TaxRate { get; set; }
|
||||||
|
|
||||||
@@ -20,6 +21,6 @@ namespace ReadmeSample.Entities
|
|||||||
|
|
||||||
[Projectable] public decimal Subtotal => Items.Sum(item => item.Product.ListPrice * item.Quantity);
|
[Projectable] public decimal Subtotal => Items.Sum(item => item.Product.ListPrice * item.Quantity);
|
||||||
[Projectable] public decimal Tax => Subtotal * TaxRate;
|
[Projectable] public decimal Tax => Subtotal * TaxRate;
|
||||||
[Projectable] public decimal GrandTotal => Subtotal + Tax;
|
[Projectable] public decimal GrandTotal => Subtotal * TaxRate;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,10 @@ namespace ReadmeSample.Extensions
|
|||||||
public static class UserExtensions
|
public static class UserExtensions
|
||||||
{
|
{
|
||||||
[Projectable]
|
[Projectable]
|
||||||
public static Order GetMostRecentOrderForUser(this User user, DateTime? cutoffDate)
|
public static Order GetMostRecentOrderForUser(this User user, bool includeUnfulfilled) =>
|
||||||
=> user.Orders.Where(x => x.CreatedDate >= cutoffDate).OrderByDescending(x => x.CreatedDate).FirstOrDefault();
|
user.Orders
|
||||||
|
.Where(x => !includeUnfulfilled ? x .FulfilledDate != null : true)
|
||||||
|
.OrderByDescending(x => x.CreatedDate)
|
||||||
|
.FirstOrDefault();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,8 @@ namespace ReadmeSample
|
|||||||
var sampleOrder = new Order {
|
var sampleOrder = new Order {
|
||||||
User = sampleUser,
|
User = sampleUser,
|
||||||
TaxRate = .19m,
|
TaxRate = .19m,
|
||||||
CreatedDate = DateTime.UtcNow,
|
CreatedDate = DateTime.UtcNow.AddDays(-1),
|
||||||
|
FulfilledDate = DateTime.UtcNow,
|
||||||
Items = new List<OrderItem> {
|
Items = new List<OrderItem> {
|
||||||
new OrderItem { Product = sampleProduct, Quantity = 5 }
|
new OrderItem { Product = sampleProduct, Quantity = 5 }
|
||||||
}
|
}
|
||||||
@@ -36,7 +37,7 @@ namespace ReadmeSample
|
|||||||
var query = dbContext.Users
|
var query = dbContext.Users
|
||||||
.Where(x => x.UserName == "Jon")
|
.Where(x => x.UserName == "Jon")
|
||||||
.Select(x => new {
|
.Select(x => new {
|
||||||
x.GetMostRecentOrderForUser(DateTime.UtcNow.AddDays(-30)).GrandTotal
|
GrandTotal = x.GetMostRecentOrderForUser(/* includeUnfulfilled: */ false).GrandTotal
|
||||||
});
|
});
|
||||||
|
|
||||||
var result = query.First();
|
var result = query.First();
|
||||||
|
|||||||
Reference in New Issue
Block a user