O'Reilly Forums: Wierd Linqpad Update Behavior - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Wierd Linqpad Update Behavior Rate Topic: -----

#1 User is offline   Terry Aney 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 19
  • Joined: 25-March 08

Posted 11 April 2008 - 12:53 PM

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).
0

#2 User is offline   Terry Aney 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 19
  • Joined: 25-March 08

Posted 11 April 2008 - 12:56 PM

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: 11 April 2008 - 12:56 PM

0

#3 User is offline   JoeAlbahari 

  • Super Veteran Member
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: Members
  • Posts: 527
  • Joined: 15-February 08
  • Gender:Male
  • Location:Perth, Australia

Posted 11 April 2008 - 05:02 PM

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
0

#4 User is offline   Terry Aney 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 19
  • Joined: 25-March 08

Posted 11 April 2008 - 07:45 PM

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.
0

#5 User is offline   JoeAlbahari 

  • Super Veteran Member
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: Members
  • Posts: 527
  • Joined: 15-February 08
  • Gender:Male
  • Location:Perth, Australia

Posted 12 April 2008 - 04:31 AM

QUOTE (Terry Aney @ Apr 12 2008, 11:45 AM) <{POST_SNAPBACK}>
You should be seeing something soon.


Cool - will look forward to seeing it!

Cheers

Joe
0

#6 User is offline   Terry Aney 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 19
  • Joined: 25-March 08

Posted 14 April 2008 - 12:33 AM

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 wink.gif

Enjoy.
0

#7 User is offline   JoeAlbahari 

  • Super Veteran Member
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: Members
  • Posts: 527
  • Joined: 15-February 08
  • Gender:Male
  • Location:Perth, Australia

Posted 16 April 2008 - 06:10 PM

QUOTE (Terry Aney @ Apr 14 2008, 04:33 PM) <{POST_SNAPBACK}>


Just read the article - it looks really useful.

Highly recommended!

Joe
0

#8 User is offline   Terry Aney 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 19
  • Joined: 25-March 08

Posted 20 April 2008 - 07:38 PM

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'.
0

#9 User is offline   sarahkim 

  • Active Member
  • PipPipPipPipPip
  • Group: Administrators
  • Posts: 157
  • Joined: 12-November 07
  • Gender:Female
  • Interests:online communities

Posted 24 April 2008 - 06:44 PM

Hi Terry,

I updated the topic title as you suggested so it's clear for other people.

Thanks for pointing that out!
Sarah
Sarah Kim
Online Community Manager
O'Reilly Media, Inc.
http://community.oreilly.com
0

#10 User is offline   Terry Aney 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 19
  • Joined: 25-March 08

Posted 04 May 2008 - 10:20 PM

QUOTE (Sarah Kim @ Apr 24 2008, 09:44 PM) <{POST_SNAPBACK}>
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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users