Linq update in for each loop odd behaviour

Posted on 2010-08-20
Last Modified: 2012-05-10
In the attached code snippet I have a for each loop on a linq result set.

Each loop updates a LocalPrice field and then calls CalculateRowTotals which is a routine in the partial class of the order details linq entity.

CalculateRowTotal simply updates the totals in the row based on the new LocalPrice.

For some reason CalculateRowTotals does not write the totals back to the database... the LocalPrice is changed without issue... whats going on?
'Do we have a current order

        Dim ordHead = (From p In db.bb_order_headers

                 Where p.AccountNo_FK = c.AccountNo And p.OrderStatus = "N").SingleOrDefault

        If Not (ordHead Is Nothing) Then

            Dim ordDetails = From p In db.bb_order_details

                     Where p.OrderHeaderId_FK = ordHead.OrderHeaderId

                     Select p

            For Each rec As bb_order_detail In ordDetails

                'For some reason 

                rec.LocalPriceDB = c.GetLocalCurrencyPrice(rec.Price)




        End If

Open in new window

Question by:WingYip
  • 3
  • 2
LVL 19

Expert Comment

ID: 33489890
might need to be
rec.somefield = rec.CalculateRowTotals()

Author Comment

ID: 33489916
No that wouldnt work - CalculateTotal changes a number of other fields in the row - it does not return anything - its defined in the bb_order_details partial class and therefore has access to all other values in that row instance.

Thanks anway

LVL 19

Accepted Solution

amenkes earned 500 total points
ID: 33489941
Are the changes you are making on the same objectContext?

will submit changes on the context defined by db. You might be making changes on a different context other than db
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.


Author Comment

ID: 33490964
In the code, you can see that before 'rec.LocalPriceDB' is set before 'rec.CalculateRowTotals()' is called.  The db.SubmitChanges() after the loop seems to update 'rec.LocalPriceDB' without issue.  Why would 'rec.LocalPriceDB' work and CalculateRowTotals not work when they are referecing the same object.

CalculateRowTotalsis called several times elsewhere in the app and has no probllems. However thsi is the only time a for each is used to loop through records.

LVL 19

Expert Comment

ID: 33491481
Did you set it breakpoint and step through it to see if it is fact updating the order details record or just variables stored at that moment in time?
LVL 16

Expert Comment

ID: 33544736
Could it be that even though your order_headers table has a primary key set your CalculateRowTotals class is missing one? According to this article LINQ ignores the update in that case (even if it has a pk you may try deleting it and re-adding it?):

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now