-4

The "=>" way down by the ".OrderBy(p =>" has a red squiggly under it and returns this error when trying to build. What I am trying to do is conditional sort a field based on its contents. I have tried it with and without brackets. I have searched but this problem is different because of the "if" statement. I have tried an "else" statement at the end but that turns all sorts of things red.

"Not all code paths return a value in lambda expression of type 'Func'" and then after I add "else return 3" I get the error: "A lambda expression with a statement body cannot be converted to an expression tree."

public List<OESAC.Models.BoardMembers> BoardMembers { get; set; }


public async Task OnGetAsync()
{          


 BoardMembers = await _context.BoardMembers
        .Select(p => new OESAC.Models.BoardMembers
        {
            WholeName = p.WholeName,
            Office = p.Office,
            Email = p.Email,
            Representing = p.Representing,
            WebDisplaySection = p.WebDisplaySection,
            Employer = p.Employer
        }).Where(p => p.WebDisplaySection == "Officers").OrderBy(p =>
        {
            if (p.Office == "President")
                return 0;
            else if (p.Office == "Vice-President")
                return 1;
            else if (p.Office == "Secretary-Treasurer")
                return 2;
        }).ToListAsync();
JustJohn
  • 1,362
  • 2
  • 22
  • 44

1 Answers1

-2

I replaced this:

.OrderBy(p =>
        {
            if (p.Office == "President")
                return 0;
            else if (p.Office == "Vice-President")
                return 1;
            else if (p.Office == "Secretary-Treasurer")
                return 2;
        }).ToListAsync();

with this:

.OrderBy(p => p.Office == "President" ? 0 : p.Office == "Vice-President" ? 1 : p.Office == "Secretary-Treasurer" ? 2 : 3).ToListAsync();

I'm hoping it is useful to someone. Never saw this solution anywhere else.

JustJohn
  • 1,362
  • 2
  • 22
  • 44