 |
| ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

April 12th, 2011, 02:14 PM
|
|
Authorized User
|
|
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
|
|
LINQ Update Statement
Hi
Does anyone know of any good web-based resources where I can get information on how to construct LINQ Update statements. I've done LINQ Select statements before with Imar's help but now I'm keen to look into LINQ Update statements instead of relying on standard SQL statements.
Thanks
Phil
|
|

April 12th, 2011, 02:58 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
You may want to look at the 101 LINQ samples: http://msdn.microsoft.com/en-us/vcsharp/aa336746
However, Updates are not really part of LINQ; they are part of technologies that use LINQ such as LINQ to SQL or Entity Framework. Often all you need to do is call SaveChanges or SubmitChanges on a context that contains a modified entity,
What exactly are you trying to update?
Imar
|
|

April 12th, 2011, 03:10 PM
|
|
Authorized User
|
|
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
|
|
LINQ to SQL
Hi Imar
Thanks for the reply. I want to update the status of a record in my SQL database which is by default set to '1' when the user interact which the system and triggers the update, I want to update the corresponding record and change its status to '3', etc.
The numerical status codes do correspond to associated status descriptions within a foreign key relationship between my tables.
I would prefer to use LINQ updates rather than standard SQL as it seems alot 'cleaner' to implement within my build if that make sense?
Thanks
Phil
|
|

April 12th, 2011, 03:42 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
I guess it all depends on what you're doing. Here's a simple example using LINQ to SQL but the idea is the same for EF (not sure which one you're using):
Code:
using (PlanetWroxDataContext context = new PlanetWroxDataContext())
{
Person myPerson = context.People.Where(p => p.Id == 123).First();
myPerson.Whatever = 3; // Update the property
context.SubmitChanges();
}
Does this help? If not, you need to be more concrete about what you're doing and how.
Cheers,
Imar
Last edited by Imar; April 17th, 2011 at 03:08 PM..
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

April 12th, 2011, 03:57 PM
|
|
Authorized User
|
|
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
|
|
LINQ to SQL
Hi Imar
Thanks for the posting, it definately helps and explains what I'm after doing.
Thanks for your help.
Phil
|
|

April 12th, 2011, 05:45 PM
|
|
Authorized User
|
|
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
|
|
LINQ to SQL
Hi Imar
I'm using Entity Framework. I can't get 'SubmitChanges' to implement with the EF and the only method I've found which is remotely the same is 'SaveChanges'. I'm assuming that both methods provide the same functionality and will update my backend SQL database?
Code:
Using myLiteratureCatalogue As New Model.LiteratureCataloguingEntities
Dim existingStatus = (From publication In myLiteratureCatalogue.Publications Where publication.PublicationID = "" Select publication).First()
' Change the status of the publication.
existingStatus.StatusID = (1)
myLiteratureCatalogue.SaveChanges()
End Using
|
|

April 13th, 2011, 03:00 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi Phil,
Quote:
|
I'm assuming that both methods provide the same functionality and will update my backend SQL database?
|
Yes, but in different frameworks. SubmitChanges is part of LINQ to SQL, while SaveChanges is part of Entity Framework 4. Both are similar ORM frameworks, where EF is getting more attention that L2S lately. I didn't know which one you were using so I mentioned both in my first post and then used L2S as an example later on.
Just realize that this code performs 2 queries: one to get the object, and another to update it. If you already had a reference to the said publication, you could update that instance instead.
Cheers,
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

April 13th, 2011, 05:09 AM
|
|
Authorized User
|
|
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
|
|
LINQ to Entities
Hi Imar
Thanks very much for all the information you have provided me with. Its definately very beneficial to a novice like me who is just starting out with building ASP projects and not familiar with LINQ just the more traditional SQL.
Hopefully speak to you again soon
Phil 
|
|

April 13th, 2011, 11:46 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
You're welcome...
Imar
|
|

April 16th, 2011, 05:48 PM
|
|
Authorized User
|
|
Join Date: Feb 2011
Posts: 37
Thanks: 8
Thanked 0 Times in 0 Posts
|
|
LINQ Update
Hi Imar
Is it possible to update a SQL Server database field with a NULL value using LINQ to Entities?
I'm trying to update a UniqueIdentifier field by getting the update to empty out the GUID. Is this possible?
Phil
|
|
 |