Last active
October 15, 2017 13:17
-
-
Save lahma/8f2f6f2d22b1035f3db9c774eadbd8bf to your computer and use it in GitHub Desktop.
Projection with filtering
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Raven.Client.Documents.Linq; | |
using Xunit; | |
namespace FastTests.Client.Queries | |
{ | |
public class ProjectionParameters : RavenTestBase | |
{ | |
public class Document | |
{ | |
public string Id { get; set; } | |
public string TargetId { get; set; } | |
public decimal TargetValue { get; set; } | |
public bool Deleted { get; set; } | |
public IEnumerable<Document> SubDocuments { get; set; } | |
} | |
// would like to use strongly typed classes for results like in transformers | |
// we have all project classes defined in transformers to give strong typing (TTrasformer.Result) | |
public class Result | |
{ | |
public string TargetId { get; set; } | |
public decimal TargetValue { get; set; } | |
} | |
[Fact] | |
public void CanProjectAndFilter() | |
{ | |
using (var store = GetDocumentStore()) | |
using (var session = store.OpenSession()) | |
{ | |
var doc1 = new Document | |
{ | |
Deleted = false, | |
SubDocuments = new List<Document> | |
{ | |
new Document | |
{ | |
TargetId = "id1" | |
}, | |
new Document | |
{ | |
TargetId = "id2" | |
} | |
} | |
}; | |
var doc2 = new Document | |
{ | |
Deleted = false, | |
SubDocuments = new List<Document> | |
{ | |
new Document | |
{ | |
TargetId = "id4" | |
}, | |
new Document | |
{ | |
TargetId = "id5" | |
} | |
} | |
}; | |
var doc3 = new Document | |
{ | |
Deleted = true | |
}; | |
session.Store(doc1); | |
session.Store(doc2); | |
session.Store(doc3); | |
session.SaveChanges(); | |
var ids = new[] {doc1.Id, doc2.Id, doc3.Id}; | |
string[] targetIds = {"id2"}; | |
var projection = | |
from d in session.Query<Document>().Where(x => x.Id.In(ids)) | |
where d.Deleted == false | |
select new | |
{ | |
Id = d.Id, | |
Deleted = d.Deleted, | |
Values = d.SubDocuments | |
//.Where(x => targetIds.Length == 0 || targetIds.Contains(x.TargetId)) | |
.Select(x => new // strongly typed class not supported - Result | |
{ | |
TargetId = x.TargetId, | |
TargetValue = x.TargetValue | |
}) | |
}; | |
Console.WriteLine(projection.ToString()); | |
var result = projection.ToList(); | |
Assert.True(result.Count == 2); | |
Assert.True(result.Count(x => x.Values?.Count() == 1) == 1); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment