?
Solved

How do you update an existing record using LINQ

Posted on 2009-02-15
5
Medium Priority
?
1,073 Views
Last Modified: 2013-12-17
I am trying to perform a lookup on one table then if a record is found obtain a piece of data from one field.  Next I need to use that data in a lookup in a second table.  Based on the lookup, if a record is found, I only need to update the fields.  If there is no record found, then I need to create a new record.  I understand how to create a new record, but I am unsure on how to update a record.  Any help is greatly appreciated.
// ---------------  Write Patient Demographics Record -------------------------
 
 
 
                            //----------Code to check field length
                            //==================================
                            //PrintFields(record);    // Will report the length of all String and Binary fields that have values
                            //Console.ReadLine();   // Will pause the program now verify that non of the file are longer then define in DB
                            //===================================
                            // Now Insert the Patient Demographic By Patient Table Record
 
                            // Lookup the patient in the MPI table by the Provider NPI and Entity System
                            // acct number.  If exist, obtain MPI from record.
                            // If the MPI was obtained, update the record in the Chart Relay patient table
                            //  If no record was found in the MPI table, insert a new record into the 
                            // Chart Relay patient table.
                            // If it exist in the MPI table
 
                            // Then one if statement for each table type
                            if (type == "PATIENT")
                                 
                                 
                                 DataClasses1DataContext dcChartRelay2 = new DataClasses1DataContext();
 
                                 MPI myMPI2 = new MPI();
                                 patient myECPatient2 = new patient();
                                 doctor myECdoctor2 = new doctor();
                                 var patientlookup = from MPIREC in dcChartRelay2.MPIs
                                 where myMPI2.EntitySystemAcctNum == myECPatient2.pid &&
                                 myMPI2 = myECdoctor2.NPI
                                 select new { MPIREC };
 
                                 foreach (var patientrecord in patientlookup)
                                     // If record is found ----  update Record--not sure how to do an update
                                     // else
                                     // db.PATIENTs.InsertOnSubmit(record as PATIENT);

Open in new window

0
Comment
Question by:kwh3856
  • 3
  • 2
5 Comments
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 23646920
u can update the fields of patientrecord like
patientrecord.Name = 'Name1'
and then call the SubmitChanges of the context of patientrecord
0
 

Author Comment

by:kwh3856
ID: 23646953
Ohhh.....I thought that when I did that, I was creating a new record.
Is the only differene if there is no record found in the lookup then a new record is create?
Thanks
Kenny
 
0
 

Author Comment

by:kwh3856
ID: 23646984
I just rewrote the code....would this do what I need it to do?
 
 

// ---------------  Write Patient Demographics Record -------------------------
 
 
 
                            //----------Code to check field length
                            //==================================
                            //PrintFields(record);    // Will report the length of all String and Binary fields that have values
                            //Console.ReadLine();   // Will pause the program now verify that non of the file are longer then define in DB
                            //===================================
                            // Now Insert the Patient Demographic By Patient Table Record
 
                            // Lookup the patient in the MPI table by the Provider NPI and Entity System
                            // acct number.  If exist, obtain MPI from record.
                            // If the MPI was obtained, update the record in the Chart Relay patient table
                            //  If no record was found in the MPI table, insert a new record into the 
                            // Chart Relay patient table.
                            // If it exist in the MPI table
 
                            // Then one if statement for each table type
                            if (type == "PATIENT")
                                 
                                 
                                 DataClasses1DataContext dcChartRelay2 = new DataClasses1DataContext();
 
                                 MPI myMPI2 = new MPI();
                                 patient myECPatient2 = new patient();
                                 doctor myECdoctor2 = new doctor();
                                
                                 var patientlookup = from MPIREC in dcChartRelay2.MPIs
                                 where myMPI2.EntitySystemAcctNum == myECPatient2.pid &&
                                 myMPI2 = myECdoctor2.NPI
                                 select new { MPIREC };
 
                                 Guid getMPI;
                                 getMPI = myMPI2.MPI1;
 
 
                                 var demographlookup = from CRpatient in dcChartRelay2.PATIENTs
                                                       where CRpatient.MPI = getMPI
                                                       select (CRpatient);
                                 
                                 db.SubmitChanges();

Open in new window

0
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 2000 total points
ID: 23647040
i dont see u making any changes to ur objects here and u are also creating new objects @ line 25,26 and 27.
 
say u have the record from
var patientlookup = from MPIREC in dcChartRelay2.MPIs
                                 where myMPI2.EntitySystemAcctNum == myECPatient2.pid &&
                                 myMPI2 = myECdoctor2.NPI
                                 select new { MPIREC };

then all u got to do is
 foreach (var patientrecord in patientlookup)
{
patientrecord.ToUpdateField = 'updated value'
}
dcChartRelay2.SubmitChanges();
0
 

Author Closing Comment

by:kwh3856
ID: 31547181
gauthampj,
Thank you.  The lights just went on:)

Thanks
Kenny
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Loops Section Overview
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

839 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