Skip to content

Instantly share code, notes, and snippets.

@lahma
Last active October 23, 2017 16:50
Show Gist options
  • Save lahma/4cd06f9760bcc2db8890b2cf43313221 to your computer and use it in GitHub Desktop.
Save lahma/4cd06f9760bcc2db8890b2cf43313221 to your computer and use it in GitHub Desktop.
More wishes what filtering would allow
using System.Collections.Generic;
using System.Linq;
using Raven.Client.Documents.Linq;
using Raven.Client.Documents.Session;
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; }
}
public class Result
{
public string TargetId { get; set; }
public decimal TargetValue { get; set; }
}
Document doc1;
Document doc2;
Document doc3;
private void SetUp(IDocumentSession session)
{
doc1 = new Document
{
Deleted = false,
SubDocuments = new List<Document>
{
new Document
{
TargetId = "id1"
},
new Document
{
TargetId = "id2"
}
}
};
doc2 = new Document
{
Deleted = false,
SubDocuments = new List<Document>
{
new Document
{
TargetId = "id4"
},
new Document
{
TargetId = "id5"
}
}
};
doc3 = new Document
{
Deleted = true
};
session.Store(doc1);
session.Store(doc2);
session.Store(doc3);
session.SaveChanges();
}
[Fact]
public void CanProjectWithArrayParameters()
{
using (var store = GetDocumentStore())
using (var session = store.OpenSession())
{
SetUp(session);
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
})
};
var result = projection.ToList();
}
}
[Fact]
public void CanProjectWithListParameters()
{
using (var store = GetDocumentStore())
using (var session = store.OpenSession())
{
SetUp(session);
var ids = new[] {doc1.Id, doc2.Id, doc3.Id};
var targetIds = new List<string>
{
"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.Count == 0 || targetIds.Contains(x.TargetId))
.Select(x => new // strongly typed class not supported - Result
{
TargetId = x.TargetId,
TargetValue = x.TargetValue
})
};
var result = projection.ToList();
}
}
[Fact]
public void CanProjectWithStringParameter()
{
using (var store = GetDocumentStore())
using (var session = store.OpenSession())
{
SetUp(session);
var ids = new[] {doc1.Id, doc2.Id, doc3.Id};
var targetId = "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 => targetId == null || x.TargetId == targetId)
.Select(x => new // strongly typed class not supported - Result
{
TargetId = x.TargetId,
TargetValue = x.TargetValue
})
};
var result = projection.ToList();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment