Skip to content

Commit 6aadabf

Browse files
Ihar YakimushIhar Yakimush
authored andcommitted
support select of same parameter
1 parent 1faeaf7 commit 6aadabf

File tree

6 files changed

+59
-4
lines changed

6 files changed

+59
-4
lines changed

SolrNet.IntegrationOData/Controllers/ValuesController.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public IActionResult Get()
3333
"/api/values/1?$filter=Popularity eq null",
3434
"/api/values/1?$filter=Categories/any(c: c eq 'electronics')",
3535
"/api/values/2?$select=Id,Price,Categories",
36+
"/api/values/3?$select=Id,Price,Categories",
3637
});
3738
}
3839

@@ -71,5 +72,14 @@ public ActionResult<string> Get2(ODataQueryOptions odata)
7172

7273
return this.Ok(query.OData().ApplyQueryOptions(odata).ToJson());
7374
}
75+
76+
// GET api/values/5
77+
[HttpGet("3")]
78+
public ActionResult<string> Get3(ODataQueryOptions odata)
79+
{
80+
IQueryable<Product> query = this.Solr.AsQueryable();
81+
82+
return this.Ok(query.OData().ApplyQueryOptions(odata).ToJson());
83+
}
7484
}
7585
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace SolrNet.Linq.IntegrationTests
2+
{
3+
public class DerivedProduct : Product
4+
{
5+
public string Id2 { get; set; }
6+
}
7+
}

SolrNet.Linq.IntegrationTests/SelectTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,33 @@ public void AnonymousClassSolrResult()
138138
Assert.True(t1.NumFound > 0);
139139
}
140140

141+
[Fact]
142+
public void SelectDerivedClassCast()
143+
{
144+
IQueryable<DerivedProduct> derivedProducts = Product.SolrOperations.Value.AsQueryable().Where(p => p.Id != null)
145+
.Select(p => new DerivedProduct{Id2 = p.Id}).Where(p => p.Id2 != null);
146+
147+
IQueryable<Product> q2 = derivedProducts.Cast<Product>();
148+
149+
var t1 = q2.ToSolrQueryResults();
150+
151+
Assert.NotNull(t1);
152+
Assert.NotNull(t1[0].Id);
153+
Assert.True(t1.NumFound > 0);
154+
}
155+
156+
[Fact]
157+
public void SelectSameParameter()
158+
{
159+
var t1 = Product.SolrOperations.Value.AsQueryable().Where(p => p.Id != null)
160+
.Select(p => p).Where(p => p.Id != null)
161+
.ToSolrQueryResults();
162+
163+
Assert.NotNull(t1);
164+
Assert.NotNull(t1[0].Id);
165+
Assert.True(t1.NumFound > 0);
166+
}
167+
141168
[Fact]
142169
public void MultipleSelects()
143170
{

SolrNet.Linq/Expressions/SelectMethod.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace SolrNet.Linq.Expressions
99
public static class SelectMethod
1010
{
1111
public const string Select = nameof(Queryable.Select);
12-
public static bool TryVisitSelect(this MethodCallExpression node, SelectExpressionsCollection options, MemberContext context, out SelectContext newContext)
12+
public static bool TryVisitSelect(this MethodCallExpression node, SelectExpressionsCollection options, MemberContext context, out MemberContext newContext)
1313
{
1414
newContext = null;
1515
bool result = node.Method.DeclaringType == typeof(Queryable) && node.Method.Name == Select;
@@ -31,14 +31,20 @@ public static bool TryVisitSelect(this MethodCallExpression node, SelectExpressi
3131
newContext = new SelectContext(memberInit, context);
3232
}
3333

34+
if (lambda.Body is ParameterExpression)
35+
{
36+
newContext = context;
37+
return result;
38+
}
39+
3440
if (newContext != null)
3541
{
3642
options.Fields.Clear();
3743
SelectFieldsVisitor visitor = new SelectFieldsVisitor(context, options);
3844
visitor.Visit(lambda.Body);
3945

4046
return result;
41-
}
47+
}
4248
}
4349

4450
throw new InvalidOperationException($"Unable to translate '{Select}' method.");

SolrNet.Linq/Impl/ExecuterExtensions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ namespace SolrNet.Linq.Impl
1414
{
1515
internal static class ExecuterExtensions
1616
{
17-
public static IExecuter<TNew> ChangeType<TNew, TOld>(this IExecuter<TOld> executer, MethodCallExpression selectExpression, SelectExpressionsCollection selectExpressionsCollection)
17+
public static IExecuter<TNew> ChangeType<TNew, TOld>(
18+
this IExecuter<TOld> executer,
19+
MethodCallExpression selectExpression,
20+
SelectExpressionsCollection selectExpressionsCollection)
1821
{
1922
if (executer == null) throw new ArgumentNullException(nameof(executer));
2023
if (selectExpression == null) throw new ArgumentNullException(nameof(selectExpression));

SolrNet.Linq/SolrOperationsExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ public static IQueryable<T> AsQueryable<T>(this ISolrBasicReadOnlyOperations<T>
2020

2121
public static SolrQueryResults<T> ToSolrQueryResults<T>(this IQueryable<T> queryable)
2222
{
23-
return (SolrQueryResults<T>)queryable.Provider.Execute(queryable.Expression);
23+
object execute = queryable.Provider.Execute(queryable.Expression);
24+
25+
return (SolrQueryResults<T>)execute;
2426
}
2527

2628
public static Task<SolrQueryResults<T>> ToSolrQueryResultsAsync<T>(this IQueryable<T> queryable)

0 commit comments

Comments
 (0)