Skip to content

Commit 027b3b0

Browse files
committed
fix(IQueryableExt): do not return dynamic type
1 parent a2ab5bd commit 027b3b0

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/JsonApiDotNetCore/Extensions/IQueryableExtensions.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Linq.Expressions;
45
using System.Reflection;
@@ -125,8 +126,11 @@ public static IQueryable<TSource> Filter<TSource>(this IQueryable<TSource> sourc
125126
throw new JsonApiException("400", $"Could not cast {filterQuery.PropertyValue} to {property.PropertyType.Name}");
126127
}
127128
}
128-
public static IQueryable<dynamic> Select<TSource>(this IQueryable<TSource> source, string[] columns)
129+
public static IQueryable<TSource> Select<TSource>(this IQueryable<TSource> source, IEnumerable<string> columns)
129130
{
131+
if(columns == null || columns.Count() == 0)
132+
return source;
133+
130134
var sourceType = source.ElementType;
131135

132136
var resultType = typeof(TSource);
@@ -141,9 +145,11 @@ public static IQueryable<dynamic> Select<TSource>(this IQueryable<TSource> sourc
141145
var body = Expression.MemberInit(Expression.New(resultType), bindings);
142146

143147
// { model => new TodoItem() { Property = model.Property } }
144-
var selector = Expression.Lambda<Func<TSource, dynamic>>(body, parameter);
145-
146-
return source.Select(selector);
148+
var selector = Expression.Lambda(body, parameter);
149+
150+
return source.Provider.CreateQuery<TSource>(
151+
Expression.Call(typeof(Queryable), "Select", new Type[] { sourceType, resultType },
152+
source.Expression, Expression.Quote(selector)));
147153
}
148154
}
149155
}

0 commit comments

Comments
 (0)