Bohn
Bohn

Reputation: 26919

Why I cannot compile a LINQ command

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?

Upvotes: 0

Views: 177

Answers (2)

sll
sll

Reputation: 62544

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

Upvotes: 1

Ani
Ani

Reputation: 113442

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).

Upvotes: 3

Related Questions