Skip to content

Commit

Permalink
Simplified Expression Structure (#7973)
Browse files Browse the repository at this point in the history
* Simplified Expression Structure

* Fixed snapshots

* Fixed snapshots

* Fixed snapshots
  • Loading branch information
michaelstaib authored Jan 30, 2025
1 parent 184d60f commit 5b5397c
Show file tree
Hide file tree
Showing 29 changed files with 2,403 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,9 @@ private static Expression CreateParameter(object? value, Type type)
return converter(value);
}

private static Expression CreateAndConvertParameter<T>(object value)
private static Expression CreateAndConvertParameter<T>(T value)
{
Expression<Func<T>> lambda = () => (T)value;
Expression<Func<T>> lambda = () => value;
return lambda.Body;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async Task Paging_Empty_PagingArgs()
var result = await context.Brands.OrderBy(t => t.Name).ThenBy(t => t.Id).ToPageAsync(pagingArgs);

// Assert
await Snapshot.Create()
await CreateSnapshot()
.AddQueries(queries)
.Add(
new
Expand Down Expand Up @@ -61,7 +61,7 @@ public async Task Paging_First_5()
var result = await context.Brands.OrderBy(t => t.Name).ThenBy(t => t.Id).ToPageAsync(pagingArgs);

// Assert
await Snapshot.Create()
await CreateSnapshot()
.AddQueries(queries)
.Add(
new
Expand Down Expand Up @@ -97,7 +97,7 @@ public async Task Paging_First_5_After_Id_13()
var result = await context.Brands.OrderBy(t => t.Name).ThenBy(t => t.Id).ToPageAsync(pagingArgs);

// Assert
await Snapshot.Create()
await CreateSnapshot()
.AddQueries(queries)
.Add(
new
Expand Down Expand Up @@ -129,7 +129,7 @@ public async Task Paging_Last_5()
var result = await context.Brands.OrderBy(t => t.Name).ThenBy(t => t.Id).ToPageAsync(pagingArgs);

// Assert
await Snapshot.Create()
await CreateSnapshot()
.AddQueries(queries)
.Add(
new
Expand Down Expand Up @@ -165,7 +165,7 @@ public async Task Paging_First_5_Before_Id_96()
var result = await context.Brands.OrderBy(t => t.Name).ThenBy(t => t.Id).ToPageAsync(pagingArgs);

// Assert
await Snapshot.Create()
await CreateSnapshot()
.AddQueries(queries)
.Add(
new
Expand All @@ -186,7 +186,7 @@ public async Task BatchPaging_First_5()
{
// Arrange
#if NET8_0
var snapshot = Snapshot.Create();
var snapshot = CreateSnapshot();
#else
var snapshot = Snapshot.Create("NET9_0");
#endif
Expand Down Expand Up @@ -229,7 +229,7 @@ public async Task BatchPaging_Last_5()
{
// Arrange
#if NET8_0
var snapshot = Snapshot.Create();
var snapshot = CreateSnapshot();
#else
var snapshot = Snapshot.Create("NET9_0");
#endif
Expand Down Expand Up @@ -388,6 +388,15 @@ protected override async Task<IReadOnlyDictionary<int, Page<Product>>> LoadBatch
.ToBatchPageAsync(t => t.BrandId, pagingArgs, cancellationToken);
}
}

private static Snapshot CreateSnapshot()
{
#if NET9_0_OR_GREATER
return Snapshot.Create();
#else
return Snapshot.Create("NET8_0");
#endif
}
}

file static class Extensions
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# BatchPaging_First_5

## 1

```json
{
"First": "UHJvZHVjdCAwLTA6MQ==",
"Last": "UHJvZHVjdCAwLTE6Mg==",
"Items": [
{
"Id": 1,
"Name": "Product 0-0",
"Description": null,
"Price": 0.0,
"ImageFileName": null,
"TypeId": 1,
"Type": null,
"BrandId": 1,
"Brand": null,
"AvailableStock": 0,
"RestockThreshold": 0,
"MaxStockThreshold": 0,
"OnReorder": false
},
{
"Id": 2,
"Name": "Product 0-1",
"Description": null,
"Price": 0.0,
"ImageFileName": null,
"TypeId": 1,
"Type": null,
"BrandId": 1,
"Brand": null,
"AvailableStock": 0,
"RestockThreshold": 0,
"MaxStockThreshold": 0,
"OnReorder": false
}
]
}
```

## 2

```json
{
"First": "UHJvZHVjdCAxLTA6MTAx",
"Last": "UHJvZHVjdCAxLTE6MTAy",
"Items": [
{
"Id": 101,
"Name": "Product 1-0",
"Description": null,
"Price": 0.0,
"ImageFileName": null,
"TypeId": 1,
"Type": null,
"BrandId": 2,
"Brand": null,
"AvailableStock": 0,
"RestockThreshold": 0,
"MaxStockThreshold": 0,
"OnReorder": false
},
{
"Id": 102,
"Name": "Product 1-1",
"Description": null,
"Price": 0.0,
"ImageFileName": null,
"TypeId": 1,
"Type": null,
"BrandId": 2,
"Brand": null,
"AvailableStock": 0,
"RestockThreshold": 0,
"MaxStockThreshold": 0,
"OnReorder": false
}
]
}
```

## 3

```json
{
"First": "UHJvZHVjdCAyLTA6MjAx",
"Last": "UHJvZHVjdCAyLTE6MjAy",
"Items": [
{
"Id": 201,
"Name": "Product 2-0",
"Description": null,
"Price": 0.0,
"ImageFileName": null,
"TypeId": 1,
"Type": null,
"BrandId": 3,
"Brand": null,
"AvailableStock": 0,
"RestockThreshold": 0,
"MaxStockThreshold": 0,
"OnReorder": false
},
{
"Id": 202,
"Name": "Product 2-1",
"Description": null,
"Price": 0.0,
"ImageFileName": null,
"TypeId": 1,
"Type": null,
"BrandId": 3,
"Brand": null,
"AvailableStock": 0,
"RestockThreshold": 0,
"MaxStockThreshold": 0,
"OnReorder": false
}
]
}
```

## SQL 0

```sql
SELECT t."BrandId", t0."Id", t0."AvailableStock", t0."BrandId", t0."Description", t0."ImageFileName", t0."MaxStockThreshold", t0."Name", t0."OnReorder", t0."Price", t0."RestockThreshold", t0."TypeId"
FROM (
SELECT p."BrandId"
FROM "Products" AS p
WHERE p."BrandId" IN (1, 2, 3)
GROUP BY p."BrandId"
) AS t
LEFT JOIN (
SELECT t1."Id", t1."AvailableStock", t1."BrandId", t1."Description", t1."ImageFileName", t1."MaxStockThreshold", t1."Name", t1."OnReorder", t1."Price", t1."RestockThreshold", t1."TypeId"
FROM (
SELECT p0."Id", p0."AvailableStock", p0."BrandId", p0."Description", p0."ImageFileName", p0."MaxStockThreshold", p0."Name", p0."OnReorder", p0."Price", p0."RestockThreshold", p0."TypeId", ROW_NUMBER() OVER(PARTITION BY p0."BrandId" ORDER BY p0."Name", p0."Id") AS row
FROM "Products" AS p0
WHERE p0."BrandId" = 1 OR p0."BrandId" = 2 OR p0."BrandId" = 3
) AS t1
WHERE t1.row <= 3
) AS t0 ON t."BrandId" = t0."BrandId"
ORDER BY t."BrandId", t0."BrandId", t0."Name", t0."Id"
```

## Expression 0

```text
[Microsoft.EntityFrameworkCore.Query.EntityQueryRootExpression].Where(t => (((t.BrandId == 1) OrElse (t.BrandId == 2)) OrElse (t.BrandId == 3))).GroupBy(k => k.BrandId).Select(g => new Group`2() {Key = g.Key, Items = g.OrderBy(p => p.Name).ThenBy(p => p.Id).Take(3).ToList()})
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# BatchPaging_Last_5

## 1

```json
{
"First": "MTAw",
"Last": "OTk=",
"Items": [
{
"Id": 100,
"Name": "Product 0-99",
"Description": null,
"Price": 0.0,
"ImageFileName": null,
"TypeId": 1,
"Type": null,
"BrandId": 1,
"Brand": null,
"AvailableStock": 0,
"RestockThreshold": 0,
"MaxStockThreshold": 0,
"OnReorder": false
},
{
"Id": 99,
"Name": "Product 0-98",
"Description": null,
"Price": 0.0,
"ImageFileName": null,
"TypeId": 1,
"Type": null,
"BrandId": 1,
"Brand": null,
"AvailableStock": 0,
"RestockThreshold": 0,
"MaxStockThreshold": 0,
"OnReorder": false
}
]
}
```

## 2

```json
{
"First": "MjAw",
"Last": "MTk5",
"Items": [
{
"Id": 200,
"Name": "Product 1-99",
"Description": null,
"Price": 0.0,
"ImageFileName": null,
"TypeId": 1,
"Type": null,
"BrandId": 2,
"Brand": null,
"AvailableStock": 0,
"RestockThreshold": 0,
"MaxStockThreshold": 0,
"OnReorder": false
},
{
"Id": 199,
"Name": "Product 1-98",
"Description": null,
"Price": 0.0,
"ImageFileName": null,
"TypeId": 1,
"Type": null,
"BrandId": 2,
"Brand": null,
"AvailableStock": 0,
"RestockThreshold": 0,
"MaxStockThreshold": 0,
"OnReorder": false
}
]
}
```

## 3

```json
{
"First": "MzAw",
"Last": "Mjk5",
"Items": [
{
"Id": 300,
"Name": "Product 2-99",
"Description": null,
"Price": 0.0,
"ImageFileName": null,
"TypeId": 1,
"Type": null,
"BrandId": 3,
"Brand": null,
"AvailableStock": 0,
"RestockThreshold": 0,
"MaxStockThreshold": 0,
"OnReorder": false
},
{
"Id": 299,
"Name": "Product 2-98",
"Description": null,
"Price": 0.0,
"ImageFileName": null,
"TypeId": 1,
"Type": null,
"BrandId": 3,
"Brand": null,
"AvailableStock": 0,
"RestockThreshold": 0,
"MaxStockThreshold": 0,
"OnReorder": false
}
]
}
```

## SQL 0

```sql
SELECT t."BrandId", t0."Id", t0."AvailableStock", t0."BrandId", t0."Description", t0."ImageFileName", t0."MaxStockThreshold", t0."Name", t0."OnReorder", t0."Price", t0."RestockThreshold", t0."TypeId"
FROM (
SELECT p."BrandId"
FROM "Products" AS p
WHERE p."BrandId" IN (1, 2, 3)
GROUP BY p."BrandId"
) AS t
LEFT JOIN (
SELECT t1."Id", t1."AvailableStock", t1."BrandId", t1."Description", t1."ImageFileName", t1."MaxStockThreshold", t1."Name", t1."OnReorder", t1."Price", t1."RestockThreshold", t1."TypeId"
FROM (
SELECT p0."Id", p0."AvailableStock", p0."BrandId", p0."Description", p0."ImageFileName", p0."MaxStockThreshold", p0."Name", p0."OnReorder", p0."Price", p0."RestockThreshold", p0."TypeId", ROW_NUMBER() OVER(PARTITION BY p0."BrandId" ORDER BY p0."Id" DESC) AS row
FROM "Products" AS p0
WHERE p0."BrandId" = 1 OR p0."BrandId" = 2 OR p0."BrandId" = 3
) AS t1
WHERE t1.row <= 3
) AS t0 ON t."BrandId" = t0."BrandId"
ORDER BY t."BrandId", t0."BrandId", t0."Id" DESC
```

## Expression 0

```text
[Microsoft.EntityFrameworkCore.Query.EntityQueryRootExpression].Where(t => (((t.BrandId == 1) OrElse (t.BrandId == 2)) OrElse (t.BrandId == 3))).GroupBy(k => k.BrandId).Select(g => new Group`2() {Key = g.Key, Items = g.OrderByDescending(p => p.Id).Take(3).ToList()})
```

Loading

0 comments on commit 5b5397c

Please sign in to comment.