2009-07-29 54 views
1

我試圖做一個Visio Masters收集以下Linq查詢:爲什麼我不能在Visio Masters集合上使用Linq查詢?

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

我收到以下錯誤:

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

從閱讀解決這個錯誤似乎當詢問對象不發生實施IEnumerable<T>IQueryable<T>。這是這種情況,還是其他的?

回答

1

是的,這是因爲它不是IEnumerable<T>,IQueryable<T>並且它沒有自己寫的自定義選擇方法。

流行的相信你不要必須實現那些接口有LINQ支持,你只需要有他們編譯的方法。

這是罰款:

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

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

肯定有.ToList()不會工作雖然;)

當你解決你的問題,請嘗試使用Cast<T>()擴展方法,這將使它的IEnumerable<T>並允許您LINQ到你心中的內容。

+0

我添加了答案&當我做了,你得到你需要的東西:) – shahkalpesh 2009-07-29 03:20:03

0

根據Reflector ...

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

其他的都不是IEnumerable<T>無論是。但是,好的IEnumerable在那裏,所以你可以爲每個。

0

我無法嘗試這段代碼。

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

的主旨是用Enumerable.Cast方法,以非一般的列表轉換爲一個通用的一個。
這是否工作?

1

如異常消息所示,請明確指定範圍變量'master'的類型。

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