Skip to content

Commit 03584fa

Browse files
Ihar YakimushIhar Yakimush
authored andcommitted
support multiple selects
1 parent facf239 commit 03584fa

File tree

4 files changed

+83
-2
lines changed

4 files changed

+83
-2
lines changed

SolrNet.Linq.IntegrationTests/SelectTests.cs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Runtime.Serialization;
55
using SolrNet.Attributes;
66
using SolrNet.Commands.Parameters;
7+
using SolrNet.Exceptions;
78
using Xunit;
89

910
namespace SolrNet.Linq.IntegrationTests
@@ -38,6 +39,25 @@ public void AnonymousClass()
3839
Assert.NotNull(t1);
3940
}
4041

42+
[Fact]
43+
public void MultipleSelects()
44+
{
45+
var t1 = Product.SolrOperations.Value.AsQueryable(lo => lo.SetupQueryOptions = qo =>
46+
{
47+
Assert.Equal(1, qo.Fields.Count);
48+
Assert.Equal(3, qo.FilterQueries.Count);
49+
Assert.Equal("Id:id", qo.Fields.ElementAt(0));
50+
}).Where(p => p.Id != null)
51+
.Select(p => new {p.Id, p.Price, p.Categories, Qwe = Math.Pow(2, 2)})
52+
.Where(arg => arg.Categories.Any(s => s == "electronics"))
53+
.OrderBy(arg => arg.Id)
54+
.Select(arg => new {arg.Id})
55+
.FirstOrDefault(arg2 => arg2.Id != null);
56+
57+
Assert.NotNull(t1);
58+
}
59+
60+
4161
[Fact]
4262
public void Product2()
4363
{
@@ -53,7 +73,9 @@ public void Product2()
5373
Assert.Equal("id", qo.OrderBy.ElementAt(0).FieldName);
5474
Assert.Equal("pow(2,2)", qo.OrderBy.ElementAt(1).FieldName);
5575
Assert.Equal("pow(2,3)", qo.OrderBy.ElementAt(2).FieldName);
56-
76+
77+
Assert.Equal(2, qo.FilterQueries.Count);
78+
5779
}).Where(p => p.Id != null)
5880
.Select(p => new Product2 {Id = p.Id, Price = p.Price, Categories = p.Categories, Qwe = Math.Pow(2, 2)})
5981
.Where(arg => arg.Categories.Any(s => s == "electronics"))
@@ -78,5 +100,28 @@ public void Product2()
78100
Assert.Single(t3);
79101
Assert.Equal(t1.Id, t3.Single().Id);
80102
}
103+
104+
[Fact]
105+
public void Product2WithMemberProduct()
106+
{
107+
Assert.Throws<SolrConnectionException>(() => Product.SolrOperations.Value.AsQueryable(lo => lo.SetupQueryOptions = qo =>
108+
{
109+
Assert.Equal(4, qo.Fields.Count);
110+
Assert.Equal("Price:sum(price,1)", qo.Fields.ElementAt(0));
111+
Assert.Equal("Qwe:pow(2,2)", qo.Fields.ElementAt(1));
112+
Assert.Equal("Id:id", qo.Fields.ElementAt(2));
113+
Assert.Equal("Categories:cat", qo.Fields.ElementAt(3));
114+
115+
Assert.Equal(3, qo.OrderBy.Count);
116+
Assert.Equal("id", qo.OrderBy.ElementAt(0).FieldName);
117+
Assert.Equal("sum(price,1)", qo.OrderBy.ElementAt(1).FieldName);
118+
Assert.Equal("pow(2,3)", qo.OrderBy.ElementAt(2).FieldName);
119+
}).Where(p => p.Id != null)
120+
.Select(p =>
121+
new Product2 {Id = p.Id, Price = p.Price + 1, Categories = p.Categories, Qwe = Math.Pow(2, 2)})
122+
.Where(arg => arg.Categories.Any(s => s == "electronics"))
123+
.OrderBy(arg => arg.Id).ThenBy(arg => arg.Price).ThenBy(arg => Math.Pow(2, 3))
124+
.FirstOrDefault());
125+
}
81126
}
82127
}

SolrNet.Linq/Expressions/NodeTypeHelpers/SelectMethod.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public static bool TryVisitSelect(this MethodCallExpression node, QueryOptions o
3535

3636
if (newContext != null)
3737
{
38+
options.Fields.Clear();
3839
foreach (var pairValue in newContext.Aliases.Concat(newContext.Members))
3940
{
4041
options.Fields.Add($"{pairValue.Key.Name}:{pairValue.Value}");
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
5+
using SolrNet.Impl;
6+
7+
namespace SolrNet.Linq.Impl
8+
{
9+
public class AnonymousTypeActivator<T> : ISolrDocumentActivator<T>
10+
{
11+
public T Create()
12+
{
13+
Type type = typeof(T);
14+
15+
ConstructorInfo[] ctors = type.GetConstructors();
16+
17+
// Has parameter less ctor, so use activator
18+
if (ctors.Any(info => !info.GetParameters().Any()))
19+
{
20+
return Activator.CreateInstance<T>();
21+
}
22+
23+
// try to invoke ctor with minimum args, providing default values
24+
25+
ConstructorInfo ctor = ctors.OrderBy(info => info.GetParameters().Length).First();
26+
List<object> args = new List<object>();
27+
foreach (var p in ctor.GetParameters())
28+
{
29+
args.Add(p.ParameterType.IsValueType ? Activator.CreateInstance(p.ParameterType) : null);
30+
}
31+
32+
return (T)Activator.CreateInstance(type, args.ToArray());
33+
}
34+
}
35+
}

SolrNet.Linq/Impl/ExecuterExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static IExecuter<TNew> ChangeType<TNew, TOld>(this IExecuter<TOld> execut
2929

3030
ISolrAbstractResponseParser<TNew> parser =
3131
new DefaultResponseParser<TNew>(
32-
new SolrDocumentResponseParser<TNew>(mapper, sdpv, new SolrDocumentActivator<TNew>()));
32+
new SolrDocumentResponseParser<TNew>(mapper, sdpv, new AnonymousTypeActivator<TNew>()));
3333

3434
SolrQueryExecuter<TNew> newExecuter = new SolrQueryExecuter<TNew>(parser, connection, serializer,
3535
facetQuerySerializer,

0 commit comments

Comments
 (0)