gpt4 book ai didi

c# - Entity Framework 中的联合或连接自定义数据

转载 作者:行者123 更新时间:2023-11-30 22:13:25 28 4
gpt4 key购买 nike

嗨,我只是想在 sql 中像这样

SELECT ID,Name
FROM Countrys
UNION all
SELECT '00000000-0000-0000-0000-000000000000','All' Name
ORDER BY Id

将它与组合框上的“ALL”选项一起使用

using (GezentiEntities GE = new GezentiEntities())
{
string[,] str = new string [1,2]{ {"00000000-0000-0000-0000-000000000000", "All"} };
var obj = str.Cast<object>().ToArray();
var countries = (from c in GE.Countrys select new { c.Id, c.Name })
.Concat
(from s in obj select s);

cmbNationality.DataSource = countries.ToList();
cmbNationality.ValueMember = "Id";
cmbNationality.DisplayMember = "Name";
}

它给了我那个错误:

Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context.

最佳答案

只需将您的匿名类型查询放入一个列表中,然后使用列表的 add 方法追加一个匿名类型。

注意选择中变量的命名以及添加中的后续命名,以确保它们全部匹配。

var countries = (from c in GE.Countrys 
select new {Id = c.Id,Name = c.Name}).ToList();

countries.Add(new {Id = "00000000-0000-0000-0000-000000000000", Name = "All"});

关于c# - Entity Framework 中的联合或连接自定义数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19170722/

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