0

I am using C#, WinForms, In VS2010 Pro and trying to run one line of code:

var count = before.Count(c => c == '/');

which I got from here: How would you count occurrences of a string within a string?

but it doesn't recognize Count method on Strings, so it gives error and doesn't compile. How should I fix it? what is missing?

Community
  • 1
  • 1
Bohn
  • 26,091
  • 61
  • 167
  • 254

2 Answers2

3

Most likely, you are missing a using directive for the System.Linq namespace or less likely, a reference to the System.Core.dll assembly.

Try inserting this at the top of your file:

using System.Linq;

If that doesn't fix it, right-click on your project from "Solution Explorer", choose "Add Reference" from the context-menu and then ensure that System.Core.dll is referenced.

Also ensure that you are targeting .NET 3.5 or later (there are workarounds for .NET 2.0, such as LinqBridge).

Ani
  • 111,048
  • 26
  • 262
  • 307
  • I right click on References and try to find a System.Core, but cannot find it – Bohn Jan 27 '12 at 16:49
  • Which version of the framework are you targeting? The list will be filtered to assemblies supported by that version. – Yuck Jan 27 '12 at 16:51
  • @Yuck: I have .NT 4.0 and also SP1 of .NET 3.5 according to http://www.hanselman.com/smallestdotnet/ – Bohn Jan 27 '12 at 16:57
  • Ok, Fixed it, the problem that it was not showing it was because on Build Properties of the application it was set on .NET 3.0, Although I had even .NET 4.0 but build of project was on .NET 3.0 so it was not listing System.LINQ. Thanks – Bohn Jan 27 '12 at 17:00
1

Since Enumerable.Count() extension method is available since .NET Framework 3.5 it is possible that you've not targeted right version in your C# project or have not installed .NET Framework 3.5 at all.

MSDN, version Information:

.NET Framework
Supported in: 4, 3.5

.NET Framework Client Profile
Supported in: 4, 3.5 SP1
sll
  • 61,540
  • 22
  • 104
  • 156
  • I searched to add System.Core to references but it doesn't list it. My VS Installations is corrupted maybe? – Bohn Jan 27 '12 at 16:53
  • Thanks but I have VS2010 so I think it is .NET 4.0 actually – Bohn Jan 27 '12 at 16:54
  • Ok, Fixed it, the problem that it was not showing it was because on Build Properties of the application it was set on .NET 3.0, Although I had even .NET 4.0 but build of project was on .NET 3.0 so it was not listing System.LINQ. Thanks – Bohn Jan 27 '12 at 17:00