如何使用linq递归调用,查询过滤器和更改api调用?

问题描述 投票:1回答:1

我不能发布完整代码作为其封闭源代码,但代码看起来像这样:

var total = await apiCall(arg1, queryParams(arg1, argThatNeedsToChange)).Select(e =>
               e.Where(i => i.value == value)
                .Where(i => i => i.value == value)
                .Where(i => i => i.value == value && i => i.value2 == value2)
                .SelectMany(i => i => i.value == value)
                .Sum(i => (decimal) i.Amount))
                .Select(Convert.ToUInt64); 

这是有效的(当它没有被混淆时),但api调用返回一组不同的项目,最多100个。如果有超过100个项目与.Where过滤器匹配,则此代码不会覆盖,因此会有错误总。我需要做的是:

var total = await apiCall(arg1, queryParams(arg1, argThatNeedsToChange)).Select(e =>
                 While(e.Count % 1 == 0){ e.Concat(await ApiCall(args1, queryParams(args1, e[e.Count - 1].Valuexyz)))} // this should return 'e' concatenated with the results of the looped api calls.
                .Where(i => i.value == value)
                .Where(i => i => i.value == value)
                .Where(i => i => i.value == value && i => i.value2 == value2)
                .SelectMany(i => i => i.value == value)
                .Sum(i => (decimal) i.Amount))
                .Select(Convert.ToUInt64); 

api调用结果中最后一个对象的属性,用于后续的api调用,以便返回超过100的结果。我需要将其添加到linq以获取满足过滤器的所有项目,而不仅仅是最高的100。

我还应该注意到api调用返回使用IObservableRxNet,所以在这种情况下我等待IObservable,而不是async Task函数。异步函数使用IObservable在内部转换为Observable.FromAsync()

c# linq
1个回答
0
投票

为什么你不能把这样的东西连在一起,而不是

var allResults = new List<MyType>();
string pagingToken = null; // or whatever type this is
do{
   var result = await apiCall(arg1, queryParams(arg1, argThatNeedsToChange);
   allResults.AddRange(result); // or result.Items or whatever
   pagignToken = result.XXX; // (whatever has the paging token)
}while(pagingToken != null);

和这里

var total = allResults
.Where(i => i.value == value)
                .Where(i => i => i.value == value)
                .Where(i => i => i.value == value && i => i.value2 == value2)
                .SelectMany(i => i => i.value == value)
                .Sum(i => (decimal) i.Amount))
                .Select(Convert.ToUInt64); 
© www.soinside.com 2019 - 2024. All rights reserved.