-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathProjectionBindingExpression.cs
More file actions
127 lines (113 loc) · 4.8 KB
/
ProjectionBindingExpression.cs
File metadata and controls
127 lines (113 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace Microsoft.EntityFrameworkCore.Query;
/// <summary>
/// <para>
/// An expression that gets values from <see cref="ShapedQueryExpression.QueryExpression" /> to be used in
/// <see cref="ShapedQueryExpression.ShaperExpression" /> while creating results.
/// </para>
/// <para>
/// This type is typically used by database providers (and other extensions). It is generally
/// not used in application code.
/// </para>
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-providers">Implementation of database providers and extensions</see>
/// and <see href="https://aka.ms/efcore-docs-how-query-works">How EF Core queries work</see> for more information and examples.
/// </remarks>
public class ProjectionBindingExpression : Expression, IPrintableExpression
{
/// <summary>
/// Creates a new instance of the <see cref="ProjectionBindingExpression" /> class.
/// </summary>
/// <param name="queryExpression">The query expression to get the value from.</param>
/// <param name="projectionMember">The projection member to bind with query expression.</param>
/// <param name="type">The clr type of value being read.</param>
public ProjectionBindingExpression(
Expression queryExpression,
ProjectionMember projectionMember,
Type type)
{
QueryExpression = queryExpression;
ProjectionMember = projectionMember;
Type = type;
}
/// <summary>
/// Creates a new instance of the <see cref="ProjectionBindingExpression" /> class.
/// </summary>
/// <param name="queryExpression">The query expression to get the value from.</param>
/// <param name="index">The index to bind with query expression projection.</param>
/// <param name="type">The clr type of value being read.</param>
public ProjectionBindingExpression(
Expression queryExpression,
int index,
Type type)
{
QueryExpression = queryExpression;
Index = index;
Type = type;
}
/// <summary>
/// The query expression to bind with.
/// </summary>
public virtual Expression QueryExpression { get; }
/// <summary>
/// The projection member to bind if binding is via projection member.
/// </summary>
public virtual ProjectionMember? ProjectionMember { get; }
/// <summary>
/// The projection member to bind if binding is via projection index.
/// </summary>
public virtual int? Index { get; }
/// <inheritdoc />
public override Type Type { get; }
/// <inheritdoc />
public sealed override ExpressionType NodeType
=> ExpressionType.Extension;
/// <inheritdoc />
protected override Expression VisitChildren(ExpressionVisitor visitor)
=> this;
/// <inheritdoc />
void IPrintableExpression.Print(ExpressionPrinter expressionPrinter)
{
expressionPrinter.Append(nameof(ProjectionBindingExpression) + ": ");
if (ProjectionMember != null)
{
expressionPrinter.Append(ProjectionMember.ToString());
}
else if (Index != null)
{
expressionPrinter.Append(Index.ToString()!);
}
}
/// <summary>
/// Creates a new instance of the <see cref="ProjectionBindingExpression" /> class with a new type.
/// </summary>
/// <param name="type">The new clr type of value being read.</param>
/// <returns>A new projection binding expression with the updated type.</returns>
public virtual ProjectionBindingExpression UpdateType(Type type)
{
if (type == Type)
{
return this;
}
return Index != null
? new ProjectionBindingExpression(QueryExpression, Index.Value, type)
: new ProjectionBindingExpression(QueryExpression, ProjectionMember!, type);
}
/// <inheritdoc />
public override bool Equals(object? obj)
=> obj != null
&& (ReferenceEquals(this, obj)
|| obj is ProjectionBindingExpression projectionBindingExpression
&& Equals(projectionBindingExpression));
private bool Equals(ProjectionBindingExpression projectionBindingExpression)
=> QueryExpression.Equals(projectionBindingExpression.QueryExpression)
&& Type == projectionBindingExpression.Type
&& (ProjectionMember?.Equals(projectionBindingExpression.ProjectionMember)
?? projectionBindingExpression.ProjectionMember == null)
&& Index == projectionBindingExpression.Index;
/// <inheritdoc />
public override int GetHashCode()
=> HashCode.Combine(QueryExpression, ProjectionMember, Index);
}