diff --git a/src/YesSql.Abstractions/IQuery.cs b/src/YesSql.Abstractions/IQuery.cs
index deda013b..9611d699 100644
--- a/src/YesSql.Abstractions/IQuery.cs
+++ b/src/YesSql.Abstractions/IQuery.cs
@@ -12,14 +12,17 @@ public interface IQuery
/// Adds a filter on the document type
///
/// If false the document type won't be filtered.
+ /// The name of the collection to load the object.
/// The type of document to return
- IQuery For(bool filterType = true) where T : class;
+ IQuery For(bool filterType = true, string collection = null) where T : class;
///
/// Defines what type of index should be returned
///
+ /// The name of the collection to load the object.
/// The type of index to return
- IQueryIndex ForIndex() where T : class, IIndex;
+
+ IQueryIndex ForIndex(string collection = null) where T : class, IIndex;
///
/// Returns documents from any type
diff --git a/src/YesSql.Core/Services/DefaultQuery.cs b/src/YesSql.Core/Services/DefaultQuery.cs
index 9224001b..32e2b28a 100644
--- a/src/YesSql.Core/Services/DefaultQuery.cs
+++ b/src/YesSql.Core/Services/DefaultQuery.cs
@@ -30,7 +30,7 @@ public QueryState(ISqlBuilder sqlBuilder, IStore store, string collection)
public string _bindingName = "a1";
public Dictionary> _bindings = new Dictionary>();
- public readonly string _documentTable;
+ public string _documentTable;
public string _lastParameterName;
public ISqlBuilder _sqlBuilder;
public List> _parameterBindings;
@@ -57,6 +57,11 @@ public void FlushFilters()
_predicate = null;
}
}
+ public void SetCurrentCollection(string collection)
+ {
+ _collection = collection;
+ _documentTable = _store.Configuration.TableNameConvention.GetDocumentTable(collection);
+ }
public string GetTableAlias(string tableName)
{
@@ -1039,8 +1044,13 @@ public async Task CountAsync()
}
}
- IQuery IQuery.For(bool filterType)
+ IQuery IQuery.For(bool filterType, string collection)
{
+ if (collection != null)
+ {
+ _collection = collection;
+ _queryState.SetCurrentCollection(collection);
+ }
_queryState.GetBindings().Clear();
_queryState.AddBinding(typeof(Document));
@@ -1056,8 +1066,13 @@ IQuery IQuery.For(bool filterType)
return new Query(this);
}
- IQueryIndex IQuery.ForIndex()
+ IQueryIndex IQuery.ForIndex(string collection)
{
+ if (collection != null)
+ {
+ _collection = collection;
+ _queryState.SetCurrentCollection(collection);
+ }
_queryState.GetBindings().Clear();
_queryState.AddBinding(typeof(TIndex));
_queryState._sqlBuilder.Select();
diff --git a/src/YesSql.Core/Session.cs b/src/YesSql.Core/Session.cs
index ba3f3b9f..cf90d2b1 100644
--- a/src/YesSql.Core/Session.cs
+++ b/src/YesSql.Core/Session.cs
@@ -781,7 +781,7 @@ private void BatchCommands()
// holds the queries, parameters and actions returned by an IIndexCommand, until we know we can
// add it to a batch if it fits the limits (page size and parameters boundaries)
var localDbCommand = _connection.CreateCommand();
- var localQueries = new List();
+ var localQueries = new List();
var localActions = new List>();
var batch = new BatchCommand(_connection.CreateCommand());
diff --git a/test/YesSql.Tests/CoreTests.cs b/test/YesSql.Tests/CoreTests.cs
index 688865d9..99b57c60 100644
--- a/test/YesSql.Tests/CoreTests.cs
+++ b/test/YesSql.Tests/CoreTests.cs
@@ -4155,6 +4155,8 @@ public async Task ShouldQueryInnerSelectWithCollection()
Assert.Equal(0, await session.Query(collection: "Col1").Where(x => x.Name.IsNotInAny(y => y.Firstname)).CountAsync());
Assert.Equal(2, await session.Query("Col1").For().With().Where(x => x.Name.IsInAny(y => y.Firstname)).CountAsync());
+ Assert.Equal(2, await session.Query().For(collection:"Col1").With().Where(x => x.Name.IsInAny(y => y.Firstname)).CountAsync());
+ Assert.Equal(2, await session.Query("Col2").For(collection: "Col1").With().Where(x => x.Name.IsInAny(y => y.Firstname)).CountAsync());
}
}