更新中的IEnumerable项目属性,但属性不留设置? [英] Updating an item property within IEnumerable but the property doesn't stay set?

查看:103
本文介绍了更新中的IEnumerable项目属性,但属性不留设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表:事务和TransactionAgents。 TransactionAgents有一个外键交易称为的TransactionID。 pretty标准。

I have two tables: Transactions and TransactionAgents. TransactionAgents has a foreign key to Transactions called TransactionID. Pretty standard.

我也有这个code:

BrokerManagerDataContext DB =新BrokerManagerDataContext();

BrokerManagerDataContext db = new BrokerManagerDataContext();

        var transactions = from t in db.Transactions
                    where t.SellingPrice != 0 
                    select t;

        var taAgents = from ta in db.TransactionAgents
                       select ta;

        foreach (var transaction in transactions)
        {
            foreach(var agent in taAgents)
            {
                agent.AgentCommission = ((transaction.CommissionPercent / 100) * (agent.CommissionPercent / 100) * transaction.SellingPrice) - agent.BrokerageSplit;
            } 
        }

        dataGridView1.DataSource = taAgents;

基本上,TransactionAgent有一个名为AgentCommission属性/列,它是空在我的数据库中的所有TransactionAgents。

Basically, A TransactionAgent has a property/column named AgentCommission, which is null for all TransactionAgents in my database.

我的目标是执行你的的foreach(在taAgents VAR剂)看到数学来修补的价值为每个代理,以便它不为空。

My goal is to perform the math you see in the foreach(var agent in taAgents) to patch up the value for each agent so that it isn't null.

奇怪的是,当我运行这个code和突破点在 agent.AgentCommission =(公式)它显示正在计算AgentCommissision和对象的值被更新,但它显示在我的数据网格后(仅用于测试),它不显示它计算的值。

Oddly, when I run this code and break-point on agent.AgentCommission = (formula) it shows the value is being calculated for AgentCommissision and the object is being updated but after it displays in my datagrid (used only for testing), it does not show the value it calculated.

所以,对我来说,似乎财产不被永久的对象上设置。更重要的是,如果我有一个更新坚持这个新更新的对象到数据库中,我怀疑计算AgentCommission将被设置在那里。

So, to me, it seems that the Property isn't being permanently set on the object. What's more, If I persist this newly updated object back to the database with an update, I doubt the calculated AgentCommission will be set there.

而不必我的表设置相同的方式,有没有任何人可以看看code,看看我为什么不保留属性的值?

Without having my table set up the same way, is there anyone that can look at the code and see why I am not retaining the property's value?

推荐答案

的IEnumerable< T> ■不要的保证的是更新的值将持续整个枚举。例如,列表将返回在每次迭代同一组对象的,所以如果你更新一个属性,因此将在迭代被保存。然而,的IEnumerable 取值许多其他的实现,每次返回一组新的对象,这样做会不会保留任何更改。

IEnumerable<T>s do not guarantee that updated values will persist across enumerations. For instance, a List will return the same set of objects on every iteration, so if you update a property, it will be saved across iterations. However, many other implementations of IEnumerables return a new set of objects each time, so any changes made will not persist.

如果您需要存储和更新的结果,拉的IEnumerable&LT; T&GT; 降低到列表&LT; T&GT; 或IT项目到一个新的的IEnumerable&LT; T&GT; 与应用更改

If you need to store and update the results, pull the IEnumerable<T> down to a List<T> or project it into a new IEnumerable<T> with the changes applied.

这篇关于更新中的IEnumerable项目属性,但属性不留设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆