Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

LINQ ReQuery

Posted on 2010-08-13
6
Medium Priority
?
906 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 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 64

Expert Comment

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Loops Section Overview
Suggested Courses

783 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