I had a block of code:
List<OneFeat> Feats;
...
List<OneFeat> Results = new List<OneFeat>(0);
foreach (OneFeat Test in Feats)
if (String.Compare(Test.Name, Target, true) == 0)
Results.Add(Test);
return Results;
Resharper offered:
List<OneFeat> Results = new List<OneFeat>(0);
Results.AddRange(Feats.Where(Feat => String.Compare(Feat.Name, Target, true) == 0));
return Results;
Which of course worked. However, it’s creating a list and adding it to an empty list so I tried to simplify it to:
return Feats.Where(Feat => String.Compare(Feat.Name, Target, true) == 0));
Which won’t compile because it wants a cast. If I add the cast it fails at runtime.
Is there any way to code this without copying the list of results?
or you could add .ToList() in the end of Where statement as follows: