iTranslated by AI
The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
📌
Disabling ReSharper's Hint for Converting foreach to LINQ via .editorconfig
Introduction
private static bool HasConstructorWithPreserveAttribute(INamedTypeSymbol type)
{
foreach (var constructor in type.Constructors)
{
foreach (var attribute in constructor.GetAttributes())
{
if (attribute.AttributeClass.IsPreserveAttribute())
{
return true;
}
}
}
return false;
}
When you write C# code like the above in Rider, a suggestion is made to convert the foreach loop into an expression using LINQ, as shown below.

Then, executing the "Convert into LINQ-Expression" displayed in the Context Action converts the code as follows.
private static bool HasConstructorWithPreserveAttribute(INamedTypeSymbol type)
{
return type.Constructors.SelectMany(constructor => constructor.GetAttributes()).Any(attribute => attribute.AttributeClass.IsPreserveAttribute());
}
Regardless of which style is better, I will show you how to write an .editorconfig setting so that this ReSharper hint is no longer displayed.
Method
You can achieve this simply by setting resharper_foreach_can_be_converted_to_query_using_another_get_enumerator_highlighting to none for C# code as shown below.
[*.cs]
resharper_foreach_can_be_converted_to_query_using_another_get_enumerator_highlighting = none
Discussion