Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ bool matches = wanted.Invoke(product);

## Projections

When the result is an anonymous type, specify only the source type; the result type is inferred from the
lambda:

```csharp
var summary = Projection<Product>.Create(product => new
{
product.Name,
Total = product.Price * 2
});
```

```csharp
var summary = Projection<Product, ProductSummary>.Create(product => new ProductSummary
{
Expand Down
22 changes: 22 additions & 0 deletions src/Raffinert.Expressions/Projections/ProjectionFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Linq.Expressions;

namespace Raffinert.Expressions;

/// <summary>Provides projection creation with a source type and an inferred result type.</summary>
/// <typeparam name="TSource">The type of value supplied to the projection.</typeparam>
/// <remarks>
/// This factory is useful when the result is an anonymous type. The result type is inferred from the
/// expression, so only the source type needs to be written:
/// <c>Projection&lt;Order&gt;.Create(order =&gt; new { order.Id })</c>.
/// </remarks>
public static class Projection<TSource>
{
/// <summary>Creates a projection whose result type is inferred from the expression.</summary>
/// <typeparam name="TResult">The inferred type produced by the projection.</typeparam>
/// <param name="expression">The transformation expression represented by the projection.</param>
/// <returns>A projection that represents <paramref name="expression"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="expression"/> is null.</exception>
public static Projection<TSource, TResult> Create<TResult>(
Expression<Func<TSource, TResult>> expression) =>
Projection<TSource, TResult>.Create(expression);
}
2 changes: 1 addition & 1 deletion src/Raffinert.Expressions/Raffinert.Expressions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>Raffinert.Expressions</AssemblyName>
<RootNamespace>Raffinert.Expressions</RootNamespace>
<Version>1.0.0</Version>
<Version>1.0.1</Version>
<IsPackable>true</IsPackable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<IncludeSymbols>true</IncludeSymbols>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,12 @@ public async Task StructuralAdaptationTranslates()
var adaptedCondition = condition.AdaptSource<DbProduct>();
var adaptedProjection = projection.Adapt<DbProduct, ProductRow>();

var rows = await _db.Products
var query = _db.Products
.Where(adaptedCondition)
.Select(adaptedProjection)
.OrderBy(product => product.Name)
.ToArrayAsync();
.OrderBy(product => product.Name);

var rows = await query.ToListAsync();

Assert.Equal(
"product => (product.PriceCents > 1000) && (product.Name != \"Hidden\")",
Expand Down
25 changes: 25 additions & 0 deletions tests/Raffinert.Expressions.UnitTests/ProjectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,31 @@ public void InlineSubclassEnumerableAndQueryableProjectionWork()
Assert.Equal("Book", products.AsQueryable().Select(inline).Single());
}

[Fact]
public void SourceOnlyFactoryInfersAnonymousResultType()
{
var projection = Projection<Product>.Create(product => new
{
product.Name,
Total = product.Price * 2
});

var result = projection.Invoke(new Product { Name = "Book", Price = 12m });

Assert.Equal("Book", result.Name);
Assert.Equal(24m, result.Total);
Assert.Equal(
"""
product => new
{
Name = product.Name,
Total = product.Price * 2m
}
""",
projection.GetExpandedExpression().ToReadableString(),
ignoreLineEndingDifferences: true);
}

[Fact]
public void NullSafeNestedProjectionUsesConditionalAndDefault()
{
Expand Down