How to iterate Select and add index without AsEnumerable/Select((q, index))

59 views Asked by At

I take a list of records from DB, which I need to paginate later, but filter before. Somethings like this:

... // I'm inside another .Select()
    PaymentsReceived = p.ActivityPayments.Select(q => new
    {
        Description = q.PaymentTypeID == (int)PaymentType.ACCONTO ? labelAcconto : q.PaymentTypeID == (int)PaymentType.SINGOLOPAGAMENTO && p.PaymentMethod.ID == 1 ? labelRata : labelImporto,
        Amount = q.Amount,
        Date = q.Date,
        Paid = q.Paid
    }).Select(q => new
    {
        Description = q.Description,
        Amount = q.Amount,
        Date = q.Date,
        Paid = q.Paid,
        Visible = (Month1 == null || (q.Date.HasValue && q.Date.Value.Month >= Month1)) &&
         (Month2 == null || (q.Date.HasValue && q.Date.Value.Month <= Month2)) &&
         (Year1 == null || (q.Date.HasValue && q.Date.Value.Year >= Year1)) &&
         (Year2 == null || (q.Date.HasValue && q.Date.Value.Year <= Year2)) &&
         (InitialPayment != true || (q.Description == "Rata #1")) // here's I valutate indexing
    })
}).Where(p => p.PaymentsReceived.Any(q => q.Paid && q.Visible));

What I need is, on q.Description, add a + "#" + index where q.Description == "Rata", which goes from 0 to N (ActivityPayments its already ordered).

So a list of Description should be:

Importo
Fatturazione
Rata #1
Importo
Rata #2
Rata #3

And so on.

Can't use ToList and/or AsEnumerable (since it will download in memory such 1 million records), so I need to exclude the Select((q, index)).

Can't neither first paginate, than go ToList (because the filtering depends by the values of these records, looks at == "Rata #1"), so I need to filtrate due to index before paginate.

Are there any solution to this?

0

There are 0 answers