Home > Mobile >  How to both filter and sort on ActionResult<IEnumerable<T>>?
How to both filter and sort on ActionResult<IEnumerable<T>>?

Time:10-17

I have this IEnumerable<>, but I cannot seem to find a way to both apply the .Where() and an .OrderBy()

ActionResult<IEnumerable<Shippingschedule>> Shippingschedule = await _context.Shippingschedules.Where(x => x.Text.Contains(shipname) && (x.StartDate >= DateTime.Today.AddMonths(-3) && x.StartDate <= DateTime.Today.AddMonths(3))).ToListAsync();

I want to apply an .OrderBy(), but how?

I tried ...

        ActionResult<IEnumerable<Shippingschedule>> Shippingschedule = await _context.Shippingschedules.Where(x => x.Text.Contains(shipname) && (x.StartDate >= DateTime.Today.AddMonths(-3) && x.StartDate <= DateTime.Today.AddMonths(3))),
.OrderBy() ..

I can't get it to work

Thanks

CodePudding user response:

Stupid me, the answer was staring me in the face ...

Break up the methods.

    ActionResult<IEnumerable<Shippingschedule>> Shippingschedule = await _context.Shippingschedules
        .Where(x => x.Text.Contains(shipname) && x.StartDate >= DateTime.Today.AddMonths(-3) && x.StartDate <= DateTime.Today.AddMonths(3))
        .OrderBy(x => x.StartDate)
        .ToListAsync();
  • Related