File tree Expand file tree Collapse file tree 5 files changed +70
-3
lines changed
test/QueryBuilderByLinq.Test Expand file tree Collapse file tree 5 files changed +70
-3
lines changed Original file line number Diff line number Diff line change @@ -108,7 +108,14 @@ private SelectQuery BuildAsRoot(MethodCallExpression expression)
108
108
{
109
109
if ( from . Value is IQueryable q && q . Provider is TableQuery tq )
110
110
{
111
- sq . From ( tq . TableName ) . As ( fromAlias . Name ! ) ;
111
+ if ( tq . InnerQuery != null )
112
+ {
113
+ sq . From ( tq . InnerQuery . ToQueryAsPostgres ( ) ) . As ( fromAlias . Name ! ) ;
114
+ }
115
+ else
116
+ {
117
+ sq . From ( tq . TableName ) . As ( fromAlias . Name ! ) ;
118
+ }
112
119
}
113
120
else
114
121
{
Original file line number Diff line number Diff line change @@ -26,6 +26,11 @@ public static IQueryable<T> From<T>(string tableName)
26
26
return new Table < T > ( tableName ) ;
27
27
}
28
28
29
+ public static IQueryable < T > From < T > ( IQueryable < T > subquery )
30
+ {
31
+ return new Table < T > ( subquery ) ;
32
+ }
33
+
29
34
public static IQueryable < T > InnerJoin < T > ( Expression < Predicate < T > > condition )
30
35
{
31
36
return new Table < T > ( ) ;
Original file line number Diff line number Diff line change 1
- using System . Collections ;
1
+ using Carbunql . Clauses ;
2
+ using System . Collections ;
2
3
using System . Linq . Expressions ;
3
4
4
5
namespace QueryBuilderByLinq ;
@@ -15,6 +16,11 @@ public Table(string tableName)
15
16
Query = new TableQuery < T > ( ) { TableName = tableName } ;
16
17
}
17
18
19
+ public Table ( IQueryable < T > subquery )
20
+ {
21
+ Query = new TableQuery < T > ( ) { InnerQuery = subquery . AsQueryable ( ) } ;
22
+ }
23
+
18
24
private IQueryable < T > Query { get ; set ; }
19
25
20
26
public Type ElementType => Query . ElementType ;
Original file line number Diff line number Diff line change @@ -5,7 +5,9 @@ namespace QueryBuilderByLinq;
5
5
6
6
public abstract class TableQuery
7
7
{
8
- public string TableName { get ; set ; } = string . Empty ;
8
+ public string TableName { get ; internal set ; } = string . Empty ;
9
+
10
+ public IQueryable ? InnerQuery { get ; internal set ; }
9
11
}
10
12
11
13
public class TableQuery < T > : TableQuery , IOrderedQueryable < T > , IQueryProvider
Original file line number Diff line number Diff line change
1
+ using Carbunql ;
2
+ using Xunit . Abstractions ;
3
+ using static QueryBuilderByLinq . Sql ;
4
+
5
+ namespace QueryBuilderByLinq . Test ;
6
+
7
+ public class SubQueryTest
8
+ {
9
+ private readonly QueryCommandMonitor Monitor ;
10
+
11
+ public SubQueryTest ( ITestOutputHelper output )
12
+ {
13
+ Monitor = new QueryCommandMonitor ( output ) ;
14
+ Output = output ;
15
+ }
16
+
17
+ private ITestOutputHelper Output { get ; set ; }
18
+
19
+ [ Fact ]
20
+ public void DefaultTest ( )
21
+ {
22
+ var subq = from a in From < table_a > ( ) select new { ID = a . a_id , Text = a . text } ;
23
+ var query = from x in From ( subq ) select new { x . ID , x . Text } ;
24
+
25
+ var sq = query . ToQueryAsPostgres ( ) ;
26
+
27
+ Monitor . Log ( sq ) ;
28
+
29
+ var sql = @"
30
+ SELECT
31
+ x.ID,
32
+ x.Text
33
+ FROM
34
+ (
35
+ SELECT
36
+ a.a_id AS ID,
37
+ a.text AS Text
38
+ FROM
39
+ table_a AS a
40
+ ) AS x" ;
41
+
42
+ Assert . Equal ( 29 , sq . GetTokens ( ) . ToList ( ) . Count ) ;
43
+ Assert . Equal ( sql . ToValidateText ( ) , sq . ToText ( ) . ToValidateText ( ) ) ;
44
+ }
45
+
46
+ public record struct table_a ( int a_id , string text , int value ) ;
47
+ }
You can’t perform that action at this time.
0 commit comments