Skip to content

Commit 97dc207

Browse files
committed
Add support for String indexer property
Fixes nhibernate#838
1 parent c9e025c commit 97dc207

File tree

5 files changed

+60
-0
lines changed

5 files changed

+60
-0
lines changed

src/NHibernate.Test/Async/Linq/FunctionTests.cs

+13
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,19 @@ where e.FirstName.Substring(3) == "rew"
111111
Assert.That(query[0].FirstName, Is.EqualTo("Andrew"));
112112
}
113113

114+
[Test]
115+
public async Task GetCharsFunctionAsync()
116+
{
117+
var query = await ((
118+
from e in db.Employees
119+
where e.FirstName[2] == 'e'
120+
select e
121+
).ToListAsync());
122+
123+
Assert.That(query.Count, Is.EqualTo(1));
124+
Assert.That(query[0].FirstName, Is.EqualTo("Steven"));
125+
}
126+
114127
[Test]
115128
public async Task LeftFunctionAsync()
116129
{

src/NHibernate.Test/Linq/FunctionTests.cs

+13
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,19 @@ where e.FirstName.Substring(3) == "rew"
100100
Assert.That(query[0].FirstName, Is.EqualTo("Andrew"));
101101
}
102102

103+
[Test]
104+
public void GetCharsFunction()
105+
{
106+
var query = (
107+
from e in db.Employees
108+
where e.FirstName[2] == 'e'
109+
select e
110+
).ToList();
111+
112+
Assert.That(query.Count, Is.EqualTo(1));
113+
Assert.That(query[0].FirstName, Is.EqualTo("Steven"));
114+
}
115+
103116
[Test]
104117
public void LeftFunction()
105118
{

src/NHibernate/Linq/Functions/DefaultLinqToHqlGeneratorsRegistry.cs

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public DefaultLinqToHqlGeneratorsRegistry()
4141
this.Merge(new IndexOfGenerator());
4242
this.Merge(new ReplaceGenerator());
4343
this.Merge(new LengthGenerator());
44+
this.Merge(new GetCharsGenerator());
4445
this.Merge(new TrimGenerator());
4546
this.Merge(new MathGenerator());
4647

src/NHibernate/Linq/Functions/StringGenerator.cs

+18
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,24 @@ public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject,
183183
return treeBuilder.MethodCall("substring", stringExpr, start, length);
184184
}
185185
}
186+
187+
public class GetCharsGenerator : BaseHqlGeneratorForMethod
188+
{
189+
public GetCharsGenerator()
190+
{
191+
SupportedMethods = new[]
192+
{
193+
ReflectHelper.GetMethod<string, char>(s => s[0])
194+
};
195+
}
196+
197+
public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
198+
{
199+
var expression = visitor.Visit(targetObject).AsExpression();
200+
var index = treeBuilder.Add(visitor.Visit(arguments[0]).AsExpression(), treeBuilder.Constant(1));
201+
return treeBuilder.MethodCall("substring", expression, index, treeBuilder.Constant(1));
202+
}
203+
}
186204

187205
public class IndexOfGenerator : BaseHqlGeneratorForMethod
188206
{

src/NHibernate/Util/ReflectHelper.cs

+15
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,21 @@ public static MethodInfo GetMethod<TSource>(Expression<Action<TSource>> method)
6464
return ((MethodCallExpression)method.Body).Method;
6565
}
6666

67+
/// <summary>
68+
/// Extract the <see cref="MethodInfo"/> from a given expression.
69+
/// </summary>
70+
/// <typeparam name="TSource">The declaring-type of the method.</typeparam>
71+
/// <typeparam name="TResult">The return type of the method.</typeparam>
72+
/// <param name="method">The method.</param>
73+
/// <returns>The <see cref="MethodInfo"/> of the method.</returns>
74+
public static MethodInfo GetMethod<TSource, TResult>(Expression<Func<TSource, TResult>> method)
75+
{
76+
if (method == null)
77+
throw new ArgumentNullException(nameof(method));
78+
79+
return ((MethodCallExpression) method.Body).Method;
80+
}
81+
6782
/// <summary>
6883
/// Extract the <see cref="MethodInfo"/> from a given expression.
6984
/// </summary>

0 commit comments

Comments
 (0)