如何使用代码手动将两个值插入表中

时间:2014-08-07 06:14:43

标签: c# linq

我的桌子名为 ROLES 。我想手动插入两个值以显示在下拉列表中," - 任意 - " " - 选择角色 - " 。我已经插入" - 选择角色 - " 。但我不知道如何插入另一个" - 任何 - "这里的价值。

我的代码

return new[] { new Role { Id = 0, Name = "--Select Roles--" } }.Concat(
            (from role in _db.Roles
             where role.FunctionId == functionId
             orderby role.Name ascending
             select role)).ToList();

我真的被困在这里了。请帮我解决这个问题..

3 个答案:

答案 0 :(得分:1)

如果您真的想将这些值视为角色实体:

var result = new List<Role> {
   new Role { Id = 0, Name = "--Select Roles--" },
   new Role { Id = -1, Name = "--Any--" }
};

var roles = from role in _db.Roles
            where role.FunctionId == functionId
            orderby role.Name ascending
            select role;

result.AddRange(roles);

return result;

答案 1 :(得分:0)

var list=new[] { new Role { Id = 0, Name = "--Select Roles--" } }.Concat(
            (from role in _db.Roles
             where role.FunctionId == functionId
             orderby role.Name ascending
             select role)).ToList();

list.Insert(0,new Role { Id = -1, Name = "--Any--" });
retrun list;

答案 2 :(得分:0)

试试这个

 var returnList=from role in _db.Roles
                 where role.FunctionId == functionId
                 orderby role.Name ascending
                 select role)).ToList();



 return returnList.InsertRange(0,new List<Role>(){
                           new Role { Id = 0, Name = "--Select Roles--" },
                           new Role { Id = -1, Name = "--Any--" } 

                               })