我想用LINQ
查詢選擇從我的數據庫表2組的元素,我看到它使用UNION
我沒有太多的經驗,一個例子,但我想,也許這正是我需要的,但我得到一個我無法修復的錯誤,我不確定它是否可以修復。因此,這裏是我的查詢:的Linq UNION查詢選擇兩個元素
IList<String> materialTypes = ((from tom in context.MaterialTypes
where tom.IsActive == true
select tom.Name)
.Union(from tom in context.MaterialTypes
where tom.IsActive == true
select (tom.ID))).ToList();
其中,因爲它似乎在抱怨試圖在IQueryable
使用UNION
與IEnumarebale
。我試圖通過添加ToString()
這樣來解決這個問題 - (tom.ID).ToString
導致清潔錯誤下劃線在Visual-Studio-2010
但在運行時,我得到:
{"LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression."}
泰,Leron。
你爲什麼不選擇'new {tom.Name,tom.ID}'而不是'Union'? – 2013-02-22 10:49:29
我想使用數據作爲我的'DataSource',如果我嘗試使用它會給出一個錯誤,無法從匿名類型轉換爲字符串。如果在'.ToList()'之前添加'ToString()',我會得到另一個轉換錯誤 - 這次從'char'到'String','Union'是我在這種情況下看到的解決方案。有另一種方法嗎? – Leron 2013-02-22 10:54:48