Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 612
  • Last Modified:

Linq update in for each loop odd behaviour

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?
thanks
Wing
'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)
                rec.CalculateRowTotals()
            Next
            db.SubmitChanges()
        End If

Open in new window

0
WingYip
Asked:
WingYip
  • 3
  • 2
1 Solution
 
Adam MenkesC# ASP.NET DeveloperCommented:
rec.CalculateRowTotals()
might need to be
rec.somefield = rec.CalculateRowTotals()
0
 
WingYipAuthor Commented:
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

Wing
0
 
Adam MenkesC# ASP.NET DeveloperCommented:
Are the changes you are making on the same objectContext?

db.SubmitChanges()
will submit changes on the context defined by db. You might be making changes on a different context other than db
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
WingYipAuthor Commented:
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.

Wing
0
 
Adam MenkesC# ASP.NET DeveloperCommented:
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?
0
 
13598Commented:
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?):
http://naturalprogression.wordpress.com/2010/02/12/db-submitchanges-does-not-update-data/ 
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now