2

嗨,我只是想在 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";
}

它给了我这个错误:

无法创建“System.Object”类型的常量值。此上下文仅支持原始类型或枚举类型。

4

1 回答 1

3

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

请注意 select 中变量的命名以及 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"});
于 2013-10-03T23:48:47.190 回答