Home > OS >  Where is not a member of System.Collections.Generic.List(Of String) Linq
Where is not a member of System.Collections.Generic.List(Of String) Linq

Time:08-19

Application type is Windows Forms Application but I'm using it as a helper project in a solution so it doesn't actually have any forms in the project.

Option Infer is on. Using .net 3.5

I tried referencing System.Core, System.Data, System.Data.Linq but none worked.

Dim lis As New List(Of String)
lis.Add("orange")
lis.Where(Function(x) x = "apple").ToList()

Getting compile error:

'Where' is not a member of 'System.Collections.Generic.List(Of String)'

I've been searching for a while now. It works fine in the parent project but not in the helper project. What do I need to do to get 'Where' working on my list?

CodePudding user response:

You need both

  • a reference to System.Core (which you already have) and
  • Imports System.Linq at the top of your code file (which you are probably missing). Alternatively, you can globally import the namespace for your whole project (see below for details).

How do I know that?

The extension method you are trying to use is Enumerable.Where. Its documentation says the following:

Namespace: System.Linq
Assembly: System.Core.dll

The first line is the namespace you need to import, the second one the DLL you need to reference.

Why do I need to import the namespace?

Because you want to use it as an extension method. If you use it as a regular method (Enumerable.Where(lis, ...)), you can just use the fully qualified class name instead of importing the namespace (i.e., System.Linq.Enumerable.Where(lis, ...)). However, if you use it as an extension method (lis.Where(...)), you need to have the namespace imported.

Why don't I need that in my other project?

Visual Basic allows you to specify globally "imported namespaces" in the "References" tab of the project properties. It's possible that the namespace is imported project-wide in one of your projects but not the other one.

  • Related