Solved

LINQ ReQuery

Posted on 2010-08-13
6
880 Views
Last Modified: 2013-11-11
LINQ queries seem to run once and reuse the selected data if the parameters don't change. - even it the data underneath has changed. In this code the query data is not refreshed eventhough I have changed the data in the database and I want to get the refreshed data.

    '** Load Default MWH Data
    Private Sub LoadDefaultMWH()
        '* DataContext is dc_MD
        '* Table is MD_MeterPoints
        Dim wrkMWH As Double
        '** Load Selected Date data
        Dim qDM = From DM In dc_MD.MD_MeterPoints _
          Select DM Where DM.P_ID = 210

        For Each recDM In qDM                                      '* there is only 1 record
            wrkMWH = recDM.P_DefaultAvailibility
        Next

How can I get LINQ to go back to the database when it thinks it already has what I want?

Thanks!

0
Comment
Question by:BEDMDunphy
  • 2
  • 2
  • 2
6 Comments
 
LVL 18

Expert Comment

by:Anil Golamari
ID: 33431849
Dim qDM = From DM In dc_MD.MD_MeterPoints
                  Where DM.P_ID == 210
                  Select DM

Try this code.
0
 
LVL 18

Expert Comment

by:Anil Golamari
ID: 33431926
I miss typed = twice.
0
 

Author Comment

by:BEDMDunphy
ID: 33432402
I see that you reversed the order of the WHERE and Select... I tried it but I get the same results. As an additional clue, I only get the refreshed data in this query if I actually close the application. I can see the updated data in another application function without closint the application.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 33433225
Hi BEDMDunphy;

When you query the database and it returns records those records are saved in the DataContext. If your data in the database for those same records has changed by another process and you re-query the DB with the same DataContect as the original query the data for those records already in the DataContext will NOT be updated. If you change data in the records that are in the DataContext but have not executed a SubmitChanges the database will not be updated.

To get new changes that are in the database for records that are already in the DataContext execute a Refresh, something like the following :

DataContext.Refresh(RefreshMode.OverwriteCurrentValues, TheEntityTable)

For example

dc_MD.Refresh(RefreshMode.OverwriteCurrentValues, MD_MeterPoints)

Then the DataContext will have all the new values for the table MD_MeterPoints.

If you make changes to the DataContext you must do the following for the database to see them :

dc_MD.SubmitChanges()

Fernando
0
 

Author Comment

by:BEDMDunphy
ID: 33453338
Hi FernandoSOTO,

THANKS! For general information for other researchers on this issue - I did have to change the syntax slightly...

dc_MD.Refresh(RefreshMode.OverwriteCurrentValues, dc_MD.MD_MeterPoints)
 
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 33453862
Not a problem, glad to help.  ;=)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 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

18 Experts available now in Live!

Get 1:1 Help Now