Queryable_Extensions_Batch.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System.Linq.Expressions;
  2. using Vit.Linq.ExpressionNodes;
  3. using Vit.Linq.ExpressionNodes.ComponentModel;
  4. using Vitorm.StreamQuery;
  5. using Vitorm.StreamQuery.MethodCall;
  6. namespace Vitorm.MsTest.StreamQuery
  7. {
  8. public static partial class Queryable_Extensions_Batch
  9. {
  10. [ExpressionNode_CustomMethod]
  11. public static IEnumerable<List<Result>> Batch<Result>(this IQueryable<Result> source, int batchSize = 5000)
  12. {
  13. if (source == null)
  14. throw new ArgumentNullException(nameof(source));
  15. return source.Provider.Execute<IEnumerable<List<Result>>>(
  16. Expression.Call(
  17. null,
  18. new Func<IQueryable<Result>, int, IEnumerable<List<Result>>>(Batch<Result>).Method,
  19. source.Expression, Expression.Constant(batchSize)
  20. ));
  21. }
  22. #region StreamConvertor
  23. public static IStream Convert(MethodCallConvertArgrument methodConvertArg)
  24. {
  25. ExpressionNode_MethodCall call = methodConvertArg.node;
  26. var reader = methodConvertArg.reader;
  27. var arg = methodConvertArg.arg;
  28. //if (call.arguments?.Length != 2) return null;
  29. if (call.methodName != nameof(Batch)) return null;
  30. if (call.arguments[1].value is not int batchSize) batchSize = 5000;
  31. var source = reader.ReadStream(arg, call.arguments[0]);
  32. CombinedStream combinedStream = reader.AsCombinedStream(arg, source);
  33. combinedStream.method = call.methodName;
  34. combinedStream.methodArguments = new object[] { batchSize };
  35. return combinedStream;
  36. }
  37. #endregion
  38. }
  39. }