tags:

views:

41

answers:

4

I am trying to do the following Linq query on a Visio Masters collection:

List<string> allMasterNames = (from master in myStencil.Masters select master.NameU).ToList<string>

I get the following error:

Could not find an implementation of the query pattern for source type 'Microsoft.Office.Interop.Visio.Masters'. 'Select' not found. Consider explicitly specifying the type of the range variable 'master'.

From reading around this error seems to occur when the queried object does not implement IEnumerable<T> or IQueryable<T>. Is that the case here, or is it something else?

+1  A: 

Yes, it is because it's not IEnumerable<T>, IQueryable<T> and it doesn't have its own custom Select method written.

Contary to popular believe you don't have to implement those interfaces to have LINQ support, you just need to have the methods they compile down to.

This is fine:

public class MyClass { 
  public MyClass Select<MyClass>(Func<MyClass, MyClass> func) { ... } 
}

var tmp = from m in new MyClass()
          select m;

Sure a .ToList() wont work though ;)

As you solving your problem try using the Cast<T>() extension method, that'll make it an IEnumerable<T> and allow you to LINQ to your hearts content.

Slace
shahkalpesh
A: 

According to Reflector ...

public class MastersClass : IVMasters, Masters, EMasters_Event, IEnumerable
{
  // more
}

None of the others are IEnumerable<T> either. But, good ol IEnumerable is there, so you can for each.

JP Alioto
A: 

I cannot try this piece of code.

(from master in myStencil.Masters.Cast<Masters> select master.NameU).ToList<string>)

The gist is to use Enumerable.Cast method, to convert a non-generic list to a generic one.
Does that work?

shahkalpesh
A: 

As the exception message suggests, consider explicitly specifying the type of the range variable 'master'.

from Visio.Master master in myStencil.Masters select master.NameU
Joe Chung