Wierd Linqpad Update Behavior |
By Joseph Albahari, Ben Albahari
Price: $49.99 USD
£30.99 GBP
Cover | Table of Contents | Forum
![]() ![]() |
Wierd Linqpad Update Behavior |
Apr 11 2008, 08:53 PM
Post
#1
|
|
|
Active Member ![]() ![]() Group: Members Posts: 10 Joined: 25-March 08 Member No.: 169 |
I am working on finalizing an implementation to support functionality simliar to: http://weblogs.asp.net/jeffreyzhao/archive...expression.aspx
I have the following LINQPad select statement: CODE var posts = from p in be_Posts where p.PostID == new Guid( "fb68266f-3ed6-4572-956b-8c2c54a30928" ) select p; When I run an update batch statement (don't worry I plan on blogging about this whole thing once I've ironed out everything), essentially the SQL command text to run will be: CODE UPDATE [be_Posts] SET [Author] = @Author0 FROM [be_Posts] AS j0 INNER JOIN ( SELECT [t0].[PostID] FROM [be_Posts] AS [t0] WHERE [t0].[PostID] = @p0 ) AS j1 ON (j0.[PostID] = j1.[PostID]) -- @p0: Input UniqueIdentifier (Size = 0; Prec = 0; Scale = 0) [fb68266f-3ed6-4572-956b-8c2c54a30928] -- @Author0: Input NVarChar (Size = 8; Prec = 0; Scale = 0) [Ann Aney] -- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.21022.8 Now, if I take that *exact* SQL to QueryEx or SQL Management Studio, it runs fine, but LINQPad returns an error of: SqlException: Must declare the scalar variable "@Author0". Incorrect syntax near the keyword 'AS'. LINQPad obviously display more information I can append if needed, but that is a start. Any ideas to why it wouldn't work only in LINQPad (admittedly I've yet to write a little test app to test it in stand alone C# application). |
|
|
|
Apr 11 2008, 08:56 PM
Post
#2
|
|
|
Active Member ![]() ![]() Group: Members Posts: 10 Joined: 25-March 08 Member No.: 169 |
I 'mis-titled' this post. Sorry, dont' know if forum moderators want to fix it or not. Should be titled: Wierd Linqpad Update Behavior
This post has been edited by Terry Aney: Apr 11 2008, 08:56 PM |
|
|
|
Apr 12 2008, 01:02 AM
Post
#3
|
|
|
Active Member ![]() ![]() Group: Members Posts: 20 Joined: 16-February 08 Member No.: 90 |
Hi Terry
Maybe I'm missing something, but I don't see how that SQL could possibly execute in Management Studio or Query Express without first populating the parameters: CODE declare @Author0 nvarchar(100) declare @p0 int set @Author0 = 'Bloggs' set @p0 = 123 UPDATE [be_Posts] SET [Author] = @Author0 FROM [be_Posts] AS j0 INNER JOIN ( SELECT [t0].[PostID] FROM [be_Posts] AS [t0] WHERE [t0].[PostID] = @p0 ) AS j1 ON (j0.[PostID] = j1.[PostID]) Regards Joe |
|
|
|
Apr 12 2008, 03:45 AM
Post
#4
|
|
|
Active Member ![]() ![]() Group: Members Posts: 10 Joined: 25-March 08 Member No.: 169 |
Well you are right. I made the assumption you'd 'assume' that. However I figured out the problem. When creating a command string for DataContext.ExecuteCommand(), the parameters must be named @p0-N. I was naming them 'other than' that, but also providing the same 'new' name in the DbCommand.
However, when calling ExecuteCommand() you pass only the string and object[] of values for params and it takes the object[] and creates new parameters @p0-N. Stupid mistake on my part. I almost have this extension tested thoroughly enough to blog about. You should be seeing something soon. |
|
|
|
Apr 12 2008, 12:31 PM
Post
#5
|
|
|
Active Member ![]() ![]() Group: Members Posts: 20 Joined: 16-February 08 Member No.: 90 |
|
|
|
|
Apr 14 2008, 08:33 AM
Post
#6
|
|
|
Active Member ![]() ![]() Group: Members Posts: 10 Joined: 25-March 08 Member No.: 169 |
Joe,
Here is the article: http://www.aneyfamily.com/terryandann/post...INQ-to-SQL.aspx Also made apologies in there for ever suspecting LINQPad in the first place Enjoy. |
|
|
|
Apr 17 2008, 02:10 AM
Post
#7
|
|
|
Active Member ![]() ![]() Group: Members Posts: 20 Joined: 16-February 08 Member No.: 90 |
Just read the article - it looks really useful. Highly recommended! Joe |
|
|
|
Apr 21 2008, 03:38 AM
Post
#8
|
|
|
Active Member ![]() ![]() Group: Members Posts: 10 Joined: 25-March 08 Member No.: 169 |
Thanks Joe. Couple things. First, is it worth you changing the title of the post as it is a bit misleading? Not sure if you want to or not.
More importantly, I've found and posted a new fix in the code. You can read about the issue and the fix at LINQ to SQL Batch Updates/Deletes: Fix for 'Could not translate expression'. |
|
|
|
Apr 25 2008, 02:44 AM
Post
#9
|
|
|
Active Member ![]() ![]() ![]() Group: Administrators Posts: 78 Joined: 12-November 07 Member No.: 6 |
Hi Terry,
I updated the topic title as you suggested so it's clear for other people. Thanks for pointing that out! Sarah |
|
|
|
May 5 2008, 06:20 AM
Post
#10
|
|
|
Active Member ![]() ![]() Group: Members Posts: 10 Joined: 25-March 08 Member No.: 169 |
Hi Terry, I updated the topic title as you suggested so it's clear for other people. Thanks for pointing that out! Sarah I don't even remember what the 'original' title was, but I think you updated it to 'Wierd Linqpad Update Behavior' (or at least that is title currently showing). Nothing really wierd/wrong with LINQPad...I made a mistake. The post has evolved more into a 'Batch Updates and Deletes with LINQPad/LINQ to SQL'. Again, you can review if you want to change it to something along those lines. |
|
|
|
![]() ![]() |
|
Lo-Fi Version | Time is now: 16th May 2008 - 11:02 AM |