Skip to content

Commit

Permalink
Fixes SelectMany,
Browse files Browse the repository at this point in the history
  • Loading branch information
matteobortolazzo committed May 1, 2019
1 parent 2f55786 commit 9f3d69d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
20 changes: 18 additions & 2 deletions src/CouchDB.Driver/CouchQueryProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ bool IsCorrectMethod(MethodInfo m)
{
return false;
}

if (methodInfo.ReturnType != m.ReturnType)
{
if (!methodInfo.ReturnType.IsGenericParameter && !typeof(IQueryable<>).IsAssignableFrom(methodInfo.ReturnType) &&
!m.ReturnType.IsGenericParameter && !typeof(IEnumerable<>).IsAssignableFrom(m.ReturnType))
{
return false;
}
}

for (var i = 1; i < parameters.Length; i++)
{
Expression currentExpression = methodArguments[i];
Expand All @@ -176,9 +186,14 @@ bool IsCorrectMethod(MethodInfo m)
ReadOnlyCollection<ParameterExpression> lambdaParameters = l.Parameters;
Type lambdaReturnType = l.ReturnType;

if (currentParamType.Length - 1 > lambdaParameters.Count)
{
return false;
}

// The return type must be the same
var enumerableReturnType = currentParamType[currentParamType.Length - 1];
if (!enumerableReturnType.IsGenericParameter && enumerableReturnType != lambdaReturnType)
if (!enumerableReturnType.IsGenericType && !enumerableReturnType.IsGenericParameter && enumerableReturnType != lambdaReturnType)
{
return false;
}
Expand All @@ -202,7 +217,8 @@ bool IsCorrectMethod(MethodInfo m)
return true;
}

MethodInfo enumarableMethod = typeof(Enumerable).GetMethods().SingleOrDefault(IsCorrectMethod);
var enumarableMethods = typeof(Enumerable).GetMethods().Where(IsCorrectMethod).ToList();
MethodInfo enumarableMethod = enumarableMethods.First();
if (enumarableMethod == null)
{
throw new NotSupportedException($"The method '{methodInfo.Name}' is not supported");
Expand Down
5 changes: 3 additions & 2 deletions tests/CouchDB.Driver.E2ETests/Client_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ public async Task InMemoryLINQ()
Rebel luke = await rebels.CreateAsync(new Rebel { Name = "Luke", Age = 19 }).ConfigureAwait(false);
Assert.Equal("Luke", luke.Name);

luke = rebels
var x = rebels
.Where(c => c.Name == "Luke").Take(1) // Couch query
.GroupBy(e => e.Name).OrderBy(g => g.Key).First().First(); // In-memory
.GroupBy(e => e.Name).OrderBy(g => g.Key).SelectMany(g => g)
.Average(g => g.Age); // In-memory
Assert.Equal(19, luke.Age);

await client.DeleteDatabaseAsync<Rebel>().ConfigureAwait(false);
Expand Down

0 comments on commit 9f3d69d

Please sign in to comment.