首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于选择两个元素的Linq联合查询

用于选择两个元素的Linq联合查询
EN

Stack Overflow用户
提问于 2013-02-22 18:48:35
回答 1查看 135.3K关注 0票数 26

我想使用LINQ查询从我的数据库表中选择2个元素,我看到了一个使用UNION的例子,我没有太多的经验,但我认为这可能是我需要的,但我得到了一个错误,我不能修复,我不确定它是否无论如何都可以修复。所以我的问题是:

代码语言:javascript
复制
    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();

这似乎是在抱怨试图通过IEnumarebaleIQueryable上使用UNION。我试图通过像这样添加ToString() - (tom.ID).ToString来解决这个问题,这会清除Visual-Studio-2010中的错误下划线,但在运行时我得到:

代码语言:javascript
复制
{"LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression."}

泰,勒隆。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-22 18:55:02

编辑:

好了,我找到了LINQtoEF中int.ToString()失败的原因,请阅读这篇文章:Problem with converting int to string in Linq to entities

这对我来说是可行的:

代码语言:javascript
复制
        List<string> materialTypes = (from u in result.Users
                                      select u.LastName)
                       .Union(from u in result.Users
                               select SqlFunctions.StringConvert((double) u.UserId)).ToList();

在你的电脑上应该是这样的:

代码语言:javascript
复制
    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();

谢谢,我今天学到了一些东西:)

票数 45
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15022405

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档