Skip to main content

RCS1077: Optimize LINQ method call

Properties

PropertyValue
Default SeverityInfo
Minimum language version-

Examples

Example #1

diagnostic.cs
bool any = items.Where(predicate).Any();
fix.cs
bool any = items.Any(predicate);

Example #2

diagnostic.cs
int max = items.Select(selector).Max();
fix.cs
int max = items.Max(selector);

Example #3

diagnostic.cs
IEnumerable<Foo> x = items.Where(f => f is Foo).Cast<Foo>();
fix.cs
IEnumerable<Foo> x = items.OfType<Foo>();

Example #4

diagnostic.cs
bool x = items.Where((f) => Foo1(f)).Any(f => Foo2(f));
fix.cs
bool x = items.Any((f) => Foo1(f) && Foo2(f));

Example #5

diagnostic.cs
IEnumerable<object> x = items.Select(f => (object)f);
fix.cs
IEnumerable<object> x = items.Cast<object>();

Example #6

diagnostic.cs
bool x = items.FirstOrDefault((f) => Foo(f)) != null;
fix.cs
bool x = items.Any((f) => Foo(f));

Example #7

diagnostic.cs
bool x = items.FirstOrDefault() != null;
fix.cs
bool x = items.Any();

Example #8

diagnostic.cs
if (enumerable.Count() != 0)
{
}
fix.cs
if (enumerable.Any())
{
}

Example #9

diagnostic.cs
if (list.Count() == 1)
{
}
fix.cs
if (list.Count == 1)
{
}

Example #10

diagnostic.cs
var stack = new Stack<object>();
// ...
object x = stack.First();
fix.cs
var stack = new Stack<object>();
// ...
object x = stack.Peek();

Example #11

diagnostic.cs
var queue = new Queue<object>();
// ...
object x = queue.First();
fix.cs
var queue = new Queue<object>();
// ...
object x = queue.Peek();

Example #12

diagnostic.cs
enumerable.Any() ? enumerable.First() : default
fix.cs
enumerable.FirstOrDefault()

Example #13

diagnostic.cs
enumerable.OrderBy(f => f).Reverse()
fix.cs
enumerable.OrderByDescending()

Example #14

diagnostic.cs
enumerable.SelectMany(f => f.Items).Count()
fix.cs
enumerable.Sum(f => f.Items.Count)

Example #15

diagnostic.cs
listOfT.Select(f => M(f)).ToList()
fix.cs
listOfT.ConvertAll(f => M(f))

Applies to