实体框架中的联合或concat自定义数据

时间:2013-10-03 23:20:30

标签: c# linq entity-framework union concat

嗨,我只想在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”类型的常量值。在此上下文中仅支持原始类型或枚举类型。

1 个答案:

答案 0 :(得分:3)

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

注意在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"});