[英]Linq UNION query to select two elements
我想使用LINQ
查詢從我的數據庫表中選擇2個元素,我看到一個使用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();
其中,因為它似乎在抱怨試圖使用UNION
上IQueryable
與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."}
Ty,Leron。
編輯:
好吧我發現為什么LINQtoEF中的int.ToString()失敗了,請閱讀這篇文章: 將Linq中的int轉換為實體的問題
這對我有用:
List<string> materialTypes = (from u in result.Users
select u.LastName)
.Union(from u in result.Users
select SqlFunctions.StringConvert((double) u.UserId)).ToList();
你的應該是這樣的:
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 SqlFunctions.StringConvert((double)tom.ID))).ToList();
謝謝,我今天學到了一些東西:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.