实体框架异常

alb*_*oss 2 .net entity-framework

大家好我在entityframework中有这样的代码(我想在同一时间更改很多项目我不知道我们是否可以使用这样的循环,但它会抛出这样的异常:

LINQ to Entities无法识别方法'Int32 get_Item(Int32)'方法,并且此方法无法转换为存储表达式.

码:

        try
        {
            for (int j = 0; j < ids.Count; j++)
            {
                using (OzgorenEntities2 context = new OzgorenEntities2())
                {
                        Stock st = context.Stocks.First(i => i.id == ids[j]);
                        st.stockAmount =  amounts[j];
                        context.SaveChanges();
                }
            }
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
Run Code Online (Sandbox Code Playgroud)

说实话,我搜索过,只发现转换不能在服务器端工作,但我没有将它转换成可能对我有用的解决方案?

谢谢

chr*_*tor 5

尝试为给定索引处的ids值引入变量

            using (OzgorenEntities2 context = new OzgorenEntities2())
            {
                    var id = ids[j];
                    Stock st = context.Stocks.First(i => i.id == id);
                    st.stockAmount =  amounts[j];
                    context.SaveChanges();
            }
Run Code Online (Sandbox Code Playgroud)