rspec/rules/S3169/csharp/rule.adoc

41 lines
1000 B
Plaintext

There's no point in chaining multiple ``++OrderBy++`` calls in a LINQ; only the last one will be reflected in the result because each subsequent call completely reorders the list. Thus, calling ``++OrderBy++`` multiple times is a performance issue as well, because all of the sorting will be executed, but only the result of the last sort will be kept.
Instead, use ``++ThenBy++`` for each call after the first.
== Noncompliant Code Example
----
var x = personList
.OrderBy(person => person.Age)
.OrderBy(person => person.Name) // Noncompliant
.ToList(); // x is sorted by Name, not sub-sorted
----
== Compliant Solution
----
var x = personList
.OrderBy(person => person.Age)
.ThenBy(person => person.Name)
.ToList();
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]