Skip to content

Commit 2e20261

Browse files
hazzikfredericDelaporte
authored andcommitted
NH-2319 - Add ability to query collections with AsQueryable() (#375)
* NH-2319 - Add ability to query collections with AsQueryable() * Cleanup log level handling.
1 parent f3ccc55 commit 2e20261

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+2763
-149
lines changed

Tools/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
<package id="NUnit.Extension.NUnitV2ResultWriter" version="3.6.0" targetFramework="net461" />
88
<package id="NUnit.Extension.TeamCityEventListener" version="1.0.2" targetFramework="net461" />
99
<package id="NUnit.Extension.VSProjectLoader" version="3.6.0" targetFramework="net461" />
10-
<package id="CSharpAsyncGenerator.CommandLine" version="0.4.3" targetFramework="net461" />
10+
<package id="CSharpAsyncGenerator.CommandLine" version="0.4.4" targetFramework="net461" />
1111
</packages>

doc/reference/modules/query_linq.xml

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,28 @@ using NHibernate.Linq;]]></programlisting>
4242
.Where(c => c.Name == "Max")
4343
.ToList();]]></programlisting>
4444

45+
<para>
46+
Starting with NHibernate 5.0, queries can also be created from an entity collection, with the standard
47+
Linq extension <literal>AsQueryable</literal> available from <literal>System.Linq</literal> namespace.
48+
</para>
49+
<programlisting><![CDATA[IList<Cat> whiteKittens =
50+
cat.Kittens.AsQueryable()
51+
.Where(k => k.Color == "white")
52+
.ToList();]]></programlisting>
53+
<para>
54+
This will be executed as a query on that <literal>cat</literal>'s kittens without loading the
55+
entire collection.
56+
</para>
57+
<para>
58+
If the collection is a map, call <literal>AsQueryable</literal> on its <literal>Values</literal>
59+
property.
60+
</para>
61+
<programlisting><![CDATA[IList<Cat> whiteKittens =
62+
cat.Kittens.Values.AsQueryable()
63+
.Where(k => k.Color == "white")
64+
.ToList();]]></programlisting>
65+
<para>&nbsp;</para>
66+
4567
<para>
4668
A client timeout for the query can be defined.
4769
</para>
@@ -789,4 +811,4 @@ cfg.LinqToHqlGeneratorsRegistry<ExtendedLinqToHqlGeneratorsRegistry>();
789811
</para>
790812
</sect2>
791813
</sect1>
792-
</chapter>
814+
</chapter>

src/NHibernate.Test/Async/NHSpecificTest/Logs/LogsFixture.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public class TextLogSpy : IDisposable
7272
private readonly TextWriterAppender appender;
7373
private readonly Logger loggerImpl;
7474
private readonly StringBuilder stringBuilder;
75+
private readonly Level previousLevel;
7576

7677
public TextLogSpy(string loggerName, string pattern)
7778
{
@@ -84,6 +85,7 @@ public TextLogSpy(string loggerName, string pattern)
8485
};
8586
loggerImpl = (Logger)LogManager.GetLogger(typeof(LogsFixtureAsync).Assembly, loggerName).Logger;
8687
loggerImpl.AddAppender(appender);
88+
previousLevel = loggerImpl.Level;
8789
loggerImpl.Level = Level.All;
8890
}
8991

@@ -98,9 +100,10 @@ public string[] Events
98100
public void Dispose()
99101
{
100102
loggerImpl.RemoveAppender(appender);
103+
loggerImpl.Level = previousLevel;
101104
}
102105
}
103106
}
104107

105108

106-
}
109+
}

src/NHibernate.Test/Async/NHSpecificTest/NH1612/NativeSqlCollectionLoaderFixture.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ public async Task LoadCompositeElementsWithWithSimpleHbmAliasInjectionAsync()
5151
Country country = await (LoadCountryWithNativeSQLAsync(CreateCountry(stats), "LoadAreaStatisticsWithSimpleHbmAliasInjection"));
5252

5353
Assert.That(country, Is.Not.Null);
54-
Assert.That((ICollection) country.Statistics.Keys, Is.EquivalentTo((ICollection) stats.Keys), "Keys");
55-
Assert.That((ICollection) country.Statistics.Values, Is.EquivalentTo((ICollection) stats.Values), "Elements");
54+
Assert.That(country.Statistics.Keys, Is.EquivalentTo(stats.Keys), "Keys");
55+
Assert.That(country.Statistics.Values, Is.EquivalentTo(stats.Values), "Elements");
5656
await (CleanupWithPersonsAsync());
5757
}
5858

@@ -63,8 +63,8 @@ public async Task LoadCompositeElementsWithWithComplexHbmAliasInjectionAsync()
6363
Country country = await (LoadCountryWithNativeSQLAsync(CreateCountry(stats), "LoadAreaStatisticsWithComplexHbmAliasInjection"));
6464

6565
Assert.That(country, Is.Not.Null);
66-
Assert.That((ICollection) country.Statistics.Keys, Is.EquivalentTo((ICollection) stats.Keys), "Keys");
67-
Assert.That((ICollection) country.Statistics.Values, Is.EquivalentTo((ICollection) stats.Values), "Elements");
66+
Assert.That(country.Statistics.Keys, Is.EquivalentTo(stats.Keys), "Keys");
67+
Assert.That(country.Statistics.Values, Is.EquivalentTo(stats.Values), "Elements");
6868
await (CleanupWithPersonsAsync());
6969
}
7070

@@ -75,8 +75,8 @@ public async Task LoadCompositeElementsWithWithCustomAliasesAsync()
7575
Country country = await (LoadCountryWithNativeSQLAsync(CreateCountry(stats), "LoadAreaStatisticsWithCustomAliases"));
7676

7777
Assert.That(country, Is.Not.Null);
78-
Assert.That((ICollection) country.Statistics.Keys, Is.EquivalentTo((ICollection) stats.Keys), "Keys");
79-
Assert.That((ICollection) country.Statistics.Values, Is.EquivalentTo((ICollection) stats.Values), "Elements");
78+
Assert.That(country.Statistics.Keys, Is.EquivalentTo(stats.Keys), "Keys");
79+
Assert.That(country.Statistics.Values, Is.EquivalentTo(stats.Values), "Elements");
8080

8181
await (CleanupWithPersonsAsync());
8282
}
@@ -201,8 +201,8 @@ public async Task LoadCompositeElementCollectionWithCustomLoaderAsync()
201201
{
202202
var a = await (session.GetAsync<Area>(country.Code));
203203
Assert.That(a, Is.Not.Null, "area");
204-
Assert.That((ICollection) a.Statistics.Keys, Is.EquivalentTo((ICollection) stats.Keys), "area.Keys");
205-
Assert.That((ICollection) a.Statistics.Values, Is.EquivalentTo((ICollection) stats.Values), "area.Elements");
204+
Assert.That(a.Statistics.Keys, Is.EquivalentTo(stats.Keys), "area.Keys");
205+
Assert.That(a.Statistics.Values, Is.EquivalentTo(stats.Values), "area.Elements");
206206
}
207207
await (CleanupWithPersonsAsync());
208208
}
@@ -428,4 +428,4 @@ private static IDictionary<int, AreaStatistics> CreateStatistics()
428428

429429
#endregion
430430
}
431-
}
431+
}

0 commit comments

Comments
 (0)