代码之家  ›  专栏  ›  技术社区  ›  Stfvns

无法从IEnumerable更改foreach中的值<model>

  •  -2
  • Stfvns  · 技术社区  · 6 年前

    如何在中更改对象的值 foreach IEnumerable<Model> .

    代码:

    public IEnumerable<Model> ListDaftarPjsp()
    {
        IEnumerable<Model> list = from x in db.PJSPEvaluation
                                  select new Model
                                  {
                                      foo = x.Foo,
                                      bar = x.Bar               
                                  };
        foreach (Model item in list) {
            item.condition = "example";
        }
        return list;
    }
    
    public class Model{
        public string foo{ get; set; }
        public string bar { get; set; }
        public string condition{ get; set; }
    }
    

    我已经创建了 Model . 然后我使用foreach循环结果,然后设置它。但是回报 condition 还是没变?如何在foreach中设置条件,然后返回结果

    3 回复  |  直到 6 年前
        1
  •  2
  •   Corey    6 年前

    IEnumerable<T> 是查询,而不是集合。当另一端有某种集合时,查询本身 不是集合 . 目标集合的性质将决定您是否可以修改内容。

    一般的经验法则是你不能期望 IEnumerable<t> 返回同一个对象列表两次,或者甚至期望您能够在该列表中枚举多次-对于 IEnumerable<t> 只枚举一次,拒绝枚举第二次或第三次。

    在本例中,您实际拥有的是类型为的数据库查询 IQueryable<Model> 被铸造成 IEnumerable<Model> . 它仍然是一个 iqueryable<型号> 这意味着,每次枚举它时,您将(可能)得到相同的数据列表,但在完全新的对象中。更改其中一个对象不会更改同一源记录的所有对象,也不会更改基础记录本身的内容。

    如果您试图在不更改基础记录的情况下修改返回的对象(似乎是这样),则需要将查询具体化为内存中的集合。根据您对返回数据的期望,有几种方法可以做到这一点。

    最简单的方法是使用 .ToArray() 扩展方法:

    public Model[] ListDaftarPjsp()
    {
        var query = from x in db.PJSPEvaluation
                    select new Model
                    {
                        foo = x.Foo,
                        bar = x.Bar               
                    };
    
        var list = query.ToArray();
    
        foreach (Model item in list) 
        {
            item.condition = "example";
        }
    
        return list;
    }
    

    现在记录在内存中的一个数组中,该数组的枚举可以多次返回相同的精确对象,而不是每次从数据库中获取新的数据副本。

        2
  •  1
  •   sujith karivelil    6 年前

    这里您正在尝试创建 Model 使用 LINQ ,然后迭代相同的内容,为每个项目添加一个附加属性。那么,为什么不在创建列表时添加属性,而不是添加一个附加循环呢?通过这样的尝试使事情变得简单:

    from x in db.PJSPEvaluation
    select new Model
    {
       foo = x.Foo,
       bar = x.Bar,
       condition = GetCondition(x.Foo)              
    };
    

    何处 GetCondition() 可定义为:

    private string GetCondition(int foo)
    {
       if(item.foo == 1) 
       { 
          return "a";
       }
       else if(item.foo == 2) 
       { 
          return "b";
       }
       else
       {
          return "xx";    
       }
    }
    
        3
  •  0
  •   Furkan Karacan    6 年前

    这个主题已经有了,但有更有效的方法来实现。 只需使用列表而不是数组[]。

      public List<Model> ListDaftarPjsp()
        {
            List<Model> list = from x in db.PJSPEvaluation
                                      select new Model
                                      {
                                          foo = x.Foo,
                                          bar = x.Bar               
                                      };
    
            foreach (Model item in list) 
            {
                item.condition = "example";
            }
            return list;
        }
    
        public class Model{
            public string foo{ get; set; }
            public string bar { get; set; }
            public string condition{ get; set; }
        }