If your underlying data structure has indexing, you're probably better off doing it with two queries anyway because it will be using the indexes to retrieve the matching items. If you're asking about how make it one line, you could always put the results together using a Concat
.
Otherwise, if you have examine every item, the only way I can think of to still use LINQ and make only one pass through is the following (untested and potentially dangerous):
int zeroesTaken = 0;
IEnumerable<Agent> agents = from a in builtAgents
where a.OptimPriority == 1
|| (a.OptimPriority == 0 && ++zeroesTaken <= 5)
select a;
Rather ugly and dangerous since you of course have to make sure you don't touch zeroesTaken
anywhere else until after the query has actually run. And I'm not sure it will actually work if the query has to be run more than once!
I'd feel better encapsulating the whole thing in a method that loops through each item in builtAgents
and yield return
s the matches...