OrderBy().Last() or OrderByDescending().First() performance

前端 未结 6 1983
灰色年华
灰色年华 2020-12-09 14:33

I know that this probably is micro-optimization, but still I wonder if there is any difference in using

var lastObject = myList.OrderBy(item => item.Crea         


        
6条回答
  •  余生分开走
    2020-12-09 15:23

    In both cases it depends somewhat on your underlying collections. If you have knowledge up front about how the collections look before the order and select you could choose one over the other. For example, if you know the list is usually in an ascending (or mostly ascending) sorted order you could prefer the first choice. Or if you know you have indexes on the SQL tables that are sorted ascending. Although the SQL optimizer can probably deal with that anyway.

    In a general case they are equivalent statements. You were right when you said it's micro-optimization.

提交回复
热议问题