Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Linq update in for each loop odd behaviour

Posted on 2010-08-20
6
Medium Priority
?
602 Views
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?
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
Comment
Question by:WingYip
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 19

Expert Comment

by:Adam Menkes
ID: 33489890
rec.CalculateRowTotals()
might need to be
rec.somefield = rec.CalculateRowTotals()
0
 
LVL 1

Author Comment

by:WingYip
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

Wing
0
 
LVL 19

Accepted Solution

by:
Adam Menkes earned 2000 total points
ID: 33489941
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:WingYip
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.

Wing
0
 
LVL 19

Expert Comment

by:Adam Menkes
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?
0
 
LVL 16

Expert Comment

by:13598
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?):
http://naturalprogression.wordpress.com/2010/02/12/db-submitchanges-does-not-update-data/ 
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

604 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