Check if income contains income items

I'm trying to optimize a subroutine that looks something like this (simplified):

public async Task<IEnumerable<Bar>> GetBars(ObjectId id){
    var output = new Collection<Bar>();

    var page = 1;
    var hasMore = true;

    while(hasMore) {
        var foos = await client.GetFoos(id, page);

        foreach(var foo : foos) {

            if(!Proceed(foo)) {
                hasMore = false;
                break;
            }

            output.Add(new Bar().Map(foo)
        }

        page++;

    return output;
}

      

The method that calls GetBars()

looks something like this:

public async Task<Baz> GetBaz(ObjectId id){
    var bars = await qux.GetBars();

    if(bars.Any() {
        var bazBaseData = qux.GetBazBaseData(id);
        var bazAdditionalData = qux.GetBazAdditionalData(id);

        return new Baz().Map(await bazBaseData, await bazAdditionalData, bars);
    }
}

      

GetBaz()

returns 0 to many elements. Since we missed out on several million identifiers, we initially added the operator if(bars.Any())

as an initial attempt to speed up the application.

As expected GetBars()

, it blocks the thread until it has collected all of its data (which may take a while). My idea was to use a yield return and then replace if(bars.Any())

with a check if we get at least one item, so we can run two other async methods (which also takes time to execute).

My question is how to do this. I know that System.Linq.Count()

and System.Linq.Any()

win the whole idea of the yield return, and if I check the first item in the enum, it will be removed from the enumeration.

Is there any other / better option besides adding, for example, an out parameter to GetBars()

?

TL; DR: How can I check if an enum from the returned lesson contains any objects without starting to repeat it?

+3


source to share


1 answer


For your actual question, "How to check if the income transferred from the refund contains any objects without starting to repeat it?" well, you don't.

It's simple, you can't use a period as the only thing you can do with IEnumerable is it's okay to enumerate it. Calling Any () is not a problem, as this "does" only enumerates the first element (not the entire list), but it is not possible to enumerate anything, since many ienumerables do not exist in any form other than (there can be no support collection. it is impossible to check if there is something that does not exist yet, no elements, by design it makes no sense)



Edit: also, I don't see any way out in your code, are you mixing expected and understandable concepts (completely unrelated)?

+4


source







All Articles