Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RavenDB-12056 support predicate for LINQ Count in projection #7723

Merged
merged 1 commit into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/Raven.Client/Util/JavascriptConversionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,22 @@ public override void ConvertToJavascript(JavascriptConversionContext context)
}
}
case "Count":
HandleCount(context, methodCallExpression.Arguments[0]);
return;
{
context.PreventDefault();
context.Visitor.Visit(methodCallExpression.Arguments[0]);
var writer = context.GetWriter();
using (writer.Operation(methodCallExpression))
{
if (methodCallExpression.Arguments.Count > 1)
{
writer.Write(".filter");
context.Visitor.Visit(methodCallExpression.Arguments[1]);
}

writer.Write(".length");
return;
}
}
default:
throw new NotSupportedException($"Unable to translate '{methodName}' to RQL operation because this method is not familiar to the RavenDB query provider.")
{
Expand Down
89 changes: 89 additions & 0 deletions test/SlowTests/Issues/RavenDB_12056.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System.Linq;
using FastTests;
using Xunit;

namespace SlowTests.Issues
{
public class RavenDB_12056 : RavenTestBase
{
[Fact]
public void CountWorksWithPredicate()
{
const string documentId = "document-id";

using (var store = GetDocumentStore())
{
using (var s = store.OpenSession())
{
s.Store(new Document
{
Id = documentId,
Items = new[]
{
new DocumentItem
{
Failed = null,
Result = null
},
new DocumentItem
{
Failed = true,
Result = 123
},
new DocumentItem
{
Failed = false,
Result = -123
}
}
});
s.SaveChanges();
}

using (var s = store.OpenSession())
{
var queryable = s.Query<Document>()
.Where(x => x.Id == documentId)
.Select(x => new
{
ItemsLength = x.Items.Length,
ItemsCount = x.Items.Count(),
FailedCount = x.Items.Count(i => i.Failed == true),
SuccessCount = x.Items.Count(i => i.Failed == false),
UnknownFailedCount = x.Items.Count(i => i.Failed == null),
NegativeResultCount = x.Items.Count(i => i.Result < 0),
PositiveResultCount = x.Items.Count(i => i.Result > 0),
UnknownResultCount = x.Items.Count(i => i.Result == null),
ExactResultCount = x.Items.Count(i => i.Result == 123)
});

var stats = queryable
.SingleOrDefault();

Assert.NotNull(stats);
Assert.Equal(3, stats.ItemsLength);
Assert.Equal(3, stats.ItemsCount);
Assert.Equal(1, stats.FailedCount);
Assert.Equal(1, stats.SuccessCount);
Assert.Equal(1, stats.UnknownFailedCount);
Assert.Equal(1, stats.NegativeResultCount);
Assert.Equal(1, stats.PositiveResultCount);
Assert.Equal(1, stats.UnknownResultCount);
Assert.Equal(1, stats.ExactResultCount);
}
}
}

private class Document
{
public string Id { get; set; }
public DocumentItem[] Items { get; set; }
}

private class DocumentItem
{
public bool? Failed { get; set; }
public int? Result { get; set; }
}
}
}