更新 IEnumerable 中的项目属性但该属性未设置?

更新 IEnumerable 中的项目属性但该属性未设置?

问题描述:

我有两个表:Transactions 和 TransactionAgents.TransactionAgents 有一个称为 TransactionID 的 Transactions 外键.很标准.

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

我也有这个代码:

BrokerManagerDataContext db = new 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 的属性/列,对于我的数据库中的所有 TransactionAgent,它都是 null.

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

我的目标是执行您在 foreach(var agent in taAgents) 中看到的数学,以修补每个代理的值,使其不为空.

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.

奇怪的是,当我在 agent.AgentCommission = (formula) 上运行此代码和断点时,它显示正在为 AgentCommissions 计算值并且对象正在更新,但在它显示在我的datagrid(仅用于测试),它不显示它计算的值.

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.

如果没有以同样的方式设置我的表,有没有人可以查看代码并了解为什么我没有保留属性的价值?

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保证更新的值将在枚举中持续存在.例如,List 将在每次迭代中返回相同的对象集,因此如果您更新一个属性,它将在迭代中保存.但是,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.

如果您需要存储和更新结果,请使用 .ToList()IEnumerable 下拉到 Listcode> 或使用应用了更改的 .Select() 将其投影到新的 IEnumerable 中.

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

要将其具体应用于您的代码,它看起来像这样:

To specifically apply that to your code, it would look like this:

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

var taAgents = (from ta in db.TransactionAgents
                select ta).ToList();

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;