Search code examples
c#.netlinqobject

List<object>: how to apply a "where" for some index element?


I've this kind of List of object:

.AsEnumerable().Select(p => new List<object>()
{
    p.ID,
    p.DateActivity.HasValue ? p.DateActivity.Value.ToString("yyyyMMdd") : null,
    p.DateActivity.HasValue ? p.DateActivity.Value.Month.ToString() : null,
    p.DateActivity.HasValue ? p.DateActivity.Value.Year.ToString() : null,
    p.Clinic.ID,
    p.Clinic.Name,
    p.Patient.LastName + " " + p.Patient.FirstName,
    p.Patient.Gold,
    p.Patient.PromoSmiles,
    p.TreatmentType.Description ?? "",
    p.PaymentMethod.Description ?? "",
    p.ActivityPayments.Select((q, index) => new
    {
        Description = q.PaymentTypeID == (int)PaymentType.ACCONTO ? labelAcconto : q.PaymentTypeID == (int)PaymentType.SINGOLOPAGAMENTO && p.PaymentMethod.ID == 1 ? labelRata + (index - p.ActivityPaymentsNonSinglePaymentCount + 1) : labelImporto,
        Amount = q.Amount,
        Date = q.Date,
        Paid = q.Paid
    })
}).ToList();

The last item (i.e. data[11]) is a list of objects.

I want to apply a Where before the .ToList(), so that it filters only objects of data where data[11] list have at least one Paid = true.

How can I do it?


Solution

  • Two solutions:

    1. Cast data[11] to IEnumerable<dynamic>
    .Where(data => ((IEnumerable<dynamic>)data[11])
                    .Any(q => q.Paid))
    
    1. Apply Where before Select
    .AsEnumerable()
    .Where(p => p.ActivityPayments.Any(q => q.Paid))
    .Select(...)