您可以從LINQ的與NH 4.0.0.4000以下擴展方法流結果(如果你不喜歡的反射黑客傳遞ISessionImplementor
):
public static EnumerableImpl Stream<T>(this IQueryable<T> source)
{
var provider = ((NhQueryable<T>) source).Provider as DefaultQueryProvider;
var sessionImpl = (ISessionImplementor)provider.GetType()
.GetProperty("Session", BindingFlags.NonPublic |
BindingFlags.Instance).GetValue(provider);
var expression = new NhLinqExpression(source.Expression, sessionImpl.Factory);
var query = sessionImpl.CreateQuery(expression);
query.SetParameters(expression.ParameterValuesByName);
provider.SetResultTransformerAndAdditionalCriteria(
query, expression, expression.ParameterValuesByName);
return (EnumerableImpl)((AbstractQueryImpl2)query).Enumerable();
}
private static void SetParameters(this IQuery query,
IDictionary<string, Tuple<object, IType>> parameters)
{
foreach (var parameterName in query.NamedParameters)
{
var param = parameters[parameterName];
if (param.Item1 == null)
{
if (typeof(IEnumerable).IsAssignableFrom(param.Item2.ReturnedClass) &&
param.Item2.ReturnedClass != typeof(string))
query.SetParameterList(parameterName, null, param.Item2);
else query.SetParameter(parameterName, null, param.Item2);
}
else
{
if (param.Item1 is IEnumerable && !(param.Item1 is string))
query.SetParameterList(parameterName, (IEnumerable)param.Item1);
else if (param.Item2 != null)
query.SetParameter(parameterName, param.Item1, param.Item2);
else query.SetParameter(parameterName, param.Item1);
}
}
}
你需要把它包在using語句,以確保讀寫器關閉:
using (var results = session.Query<Fark>().Take(50).Where(x => x.Enabled).Stream())
{
results.ForEach(x => writer.WriteLine(x.ToCsv()));
}
一些ADO.Net緩存結果在內存中,不知道是否該建在不 – Firo 2011-12-08 12:19:20