gpt4 book ai didi

c# - Linq UNION 查询选择两个元素

转载 作者:IT王子 更新时间:2023-10-29 04:47:06 26 4
gpt4 key购买 nike

我想使用 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();

这似乎是在提示试图在 IQueryable 上使用 IEnumarebale 使用 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."}

泰,莱伦。

最佳答案

编辑:

好的,我找到了 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();

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

关于c# - Linq UNION 查询选择两个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15022405/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com