开发者

LINQ Order by question

I have a linq query that returns a list of employees an开发者_JS百科d a job title. I need to sort it by job title but have the ones that do not have any employees in the output list first.

Example sorted:

- Driver       List{0}

- Attendant List{71}

- Pilot          List{19}

The driver is first because it has nothing in the list and then it is sorted by title.

I am just curious what do you think would be my best option to accomplish that?

Thanks


Something like:

var query = employees.OrderBy(x => x.Subordinates.Any() ? 1 : 0)
                     .ThenBy(x => x.JobTitle);

You could also use the fact that false sorts earlier than true:

var query = employees.OrderBy(x => !x.Subordinates.Any())
                     .ThenBy(x => x.JobTitle);

... but that's a little bit less obvious, IMO.


Something like:

Jobs.OrderBy(j => j.Eployees.Count()).ThenBy(j => j.Name);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜