我想使用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();这似乎是在抱怨试图通过IEnumarebale在IQueryable上使用UNION。我试图通过像这样添加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."}泰,勒隆。
发布于 2013-02-22 18:55:02
编辑:
好了,我找到了LINQtoEF中int.ToString()失败的原因,请阅读这篇文章:Problem with converting int to string in Linq to entities
这对我来说是可行的:
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();谢谢,我今天学到了一些东西:)
https://stackoverflow.com/questions/15022405
复制相似问题