Solved

LINQ ReQuery

Posted on 2010-08-13
6
885 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

776 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