Solved

LINQ ReQuery

Posted on 2010-08-13
6
876 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
Comment Utility
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
Comment Utility
I miss typed = twice.
0
 

Author Comment

by:BEDMDunphy
Comment Utility
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
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

 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Not a problem, glad to help.  ;=)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

10 Experts available now in Live!

Get 1:1 Help Now