Skip to content

Latest commit

 

History

History
254 lines (169 loc) · 3.06 KB

RCS1077.md

File metadata and controls

254 lines (169 loc) · 3.06 KB

RCS1077: Optimize LINQ method call

Property Value
Id RCS1077
Category Performance
Severity Info

Examples

Code with Diagnostic

bool any = items.Where(predicate).Any(); // RCS1077

Code with Fix

bool any = items.Any(predicate);

Code with Diagnostic

int max = items.Select(selector).Max(); // RCS1077

Code with Fix

int max = items.Max(selector);

Code with Diagnostic

IEnumerable<Foo> x = items.Where(f => f is Foo).Cast<Foo>(); // RCS1077

Code with Fix

IEnumerable<Foo> x = items.OfType<Foo>();

Code with Diagnostic

bool x = items.Where((f) => Foo1(f)).Any(f => Foo2(f)); // RCS1077

Code with Fix

bool x = items.Any((f) => Foo1(f) && Foo2(f));

Code with Diagnostic

IEnumerable<object> x = items.Select(f => (object)f); // RCS1077

Code with Fix

IEnumerable<object> x = items.Cast<object>();

Code with Diagnostic

bool x = items.FirstOrDefault((f) => Foo(f)) != null; // RCS1077

Code with Fix

bool x = items.Any((f) => Foo(f));

Code with Diagnostic

bool x = items.FirstOrDefault() != null; // RCS1077

Code with Fix

bool x = items.Any();

Code with Diagnostic

var x = list.ElementAt(1); // RCS1077

Code with Fix

var x = list[1];

Code with Diagnostic

var x = list.First(); // RCS1077

Code with Fix

var x = list[0];

Code with Diagnostic

if (enumerable.Count() != 0) // RCS1077
{
}

Code with Fix

if (enumerable.Any())
{
}

Code with Diagnostic

if (list.Count() == 1) // RCS1077
{
}

Code with Fix

if (list.Count == 1)
{
}

Code with Diagnostic

var stack = new Stack<object>();
// ...
object x = stack.First(); // RCS1077

Code with Fix

var stack = new Stack<object>();
// ...
object x = items.Peek();

Code with Diagnostic

var queue = new Queue<object>();
// ...
object x = stack.First(); // RCS1077

Code with Fix

var queue = new Queue<object>();
// ...
object x = items.Peek();

Code with Diagnostic

enumerable.Any() ? enumerable.First() : default // RCS1077

Code with Fix

enumerable.FirstOrDefault()

Code with Diagnostic

enumerable.OrderBy(f => f).Reverse() // RCS1077

Code with Fix

enumerable.OrderByDescending()

Code with Diagnostic

enumerable.SelectMany(f => f.Items).Count() // RCS1077

Code with Fix

enumerable.Sum(f => f.Items.Count)

See Also

(Generated with DotMarkdown)