This is a migrated thread and some comments may be shown as answers.

"The last accepted query piece is not from the this query"

6 Answers 100 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Ryan
Top achievements
Rank 1
Ryan asked on 12 Nov 2009, 10:40 PM
Hey,

I just upgraded to the Q3 version of the ORM. However, when I try running my code, I get the following message:

"The last accepted query piece is not from the this query"

Since I haven't changed my code, I believe this error is related to the upgrade.

Here is the code where I am getting the error:

Public Shared Function GetLMSAdminByPersNo(ByVal pScope As IObjectScope, ByVal pPersNo As ShortAs LMS_Classes.LMSAdmin 
      Dim currAdmin As LMS_Classes.LMSAdmin 
      Try 
         Dim result As IObjectScopeQuery(Of LMS_Classes.LMSAdmin) 
         result = From A In pScope.Extent(Of LMS_Classes.LMSAdmin)() _ 
                  Where A.PersonnelID = pPersNo _ 
                  Select A 
         If result.OfType(Of LMS_Classes.LMSAdmin)().ToList.Count > 0 Then 
            currAdmin = result.OfType(Of LMS_Classes.LMSAdmin)().ToList.Item(0) 
         Else 
            currAdmin = Nothing 
         End If 
      Catch ex As Exception 
         Throw New Exception("Error: " & ex.Message) 
         currAdmin = Nothing 
      End Try 
      Return currAdmin 
   End Function 


Thoughts?

Cheers,

Ryan

6 Answers, 1 is accepted

Sort by
0
Accepted
Thomas
Telerik team
answered on 13 Nov 2009, 11:39 AM
Hello Ryan,

yes, the exception is new: it checks, that the query is not containing pieces that OpenAccess didn't accept.
In your case, the exception is not really what you would expect, but the reuse of the results (actually a symbolic query) in the line with the .Item(0) leads to a reuse of an internal object causing the exception.
We will try to remove this exception, but in your case there are better ways to get the results that you want.

First, the OfType() method is kind of useless as it does not restrict the result, the result will be of the right type as the source extent has it already.
Better: Avoid the OfType() when not necessary
If result.ToList.Count > 0 Then 
            currAdmin = result.ToList.Item(0) 
         Else 
            currAdmin = Nothing 
         End If 

Second, because you are calling .ToList() two times, the query is physically executed completely two times. 
Better: Call the Count() property on the IQueryable to get a server side count first.
If result.Count() > 0 Then 
            currAdmin = result.ToList.Item(0) 
         Else 
            currAdmin = Nothing 
         End If 

Third, because the list can still be huge, but you want to have only one element out of it, use the FirstOrDefault() method, resulting in a TOP(1) query on the server.
Better: 
            currAdmin = result.FirstOrDefault() 
         End If 

Last: Which object gets returned by the server is not fixed, as the server can process your query in any order. You need to specify a result set ordering to make sure the first item is not random.
Best:
Public Shared Function GetLMSAdminByPersNo(ByVal pScope As IObjectScope, ByVal pPersNo As ShortAs LMS_Classes.LMSAdmin 
      Dim currAdmin As LMS_Classes.LMSAdmin 
      Try 
         Dim result As IObjectScopeQuery(Of LMS_Classes.LMSAdmin) 
         result = From A In pScope.Extent(Of LMS_Classes.LMSAdmin)() _ 
                  Where A.PersonnelID = pPersNo _                   
                  OrderBy A.PersonelID _
            Select A 
 
         currAdmin = result.FirstOrDefault() 
      Catch ex As Exception 
         Throw New Exception("Error: " & ex.Message) 
         currAdmin = Nothing 
      End Try 
      Return currAdmin 
   End Function 

Hope this helps understanding LINQ and improves your usage of it.

Best wishes,
Thomas
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Ryan
Top achievements
Rank 1
answered on 13 Nov 2009, 04:15 PM
Hey Thomas,

Thank you very much for your response. This is my first project involving Open Access and LINQ, so your explanation was very helpful, and it solved my issue. Thanks again!

Cheers,

Ryan
0
Ross Presser
Top achievements
Rank 1
answered on 04 Mar 2011, 04:53 AM
" but the reuse of the results (actually a symbolic query) in the line with the .Item(0) leads to a reuse of an internal object causing the exception."

Does this mean that a query can only be used once?  I am getting this error in a different sort of situation:

        Dim products = (From p In scope.Extent(Of Product)() Where p.Site Is aSite And p.IsActive = True)

        Dim result As New List(Of productInfo)
        For Each p1 In productUrlNames
            Dim productUrlName = p1
            Dim prod = (From p In products Where p.URLName = productUrlName).FirstOrDefault
            If prod Is Nothing Then
                logger.Info("Product {0} not found", productUrlName)
                Continue For
    Else
               ' do something with the product
            End if
       Next

As you can see, I set up the first part of the product query, then I want to reuse it several times to pick out individual products. Is this reuse pattern forbidden? 

0
Thomas
Telerik team
answered on 04 Mar 2011, 10:00 AM
Hi Ross Presser,

no, this is perfectly legal. However, I do not see where the Item(0) is accessed in your example.
 

Regards,
Thomas
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Ross Presser
Top achievements
Rank 1
answered on 04 Mar 2011, 05:08 PM
I don't access Item(0) -- what I posted is everything relevant; nothing else in the function (or any function called from here) accesses the products query afterwards. Still, running the loop is a repeatable way to generate this error.

I am well behind the current version - OpenAccess 2009.3.1119.2; I have avoided upgrading because my schema was set up using extensive use of the reverse mapping wizard, and from what I understand the mapping method was greatly changed a version or two after what I'm using, and it was recommended not to upgrade schemas but redo them from scratch.
0
Thomas
Telerik team
answered on 07 Mar 2011, 04:48 PM
Hi Ross Presser,

I really think there have been many good changes in the code base that it would be worthwhile to update to the newest version which does not have this issue.

Regards,
Thomas
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
Tags
General Discussions
Asked by
Ryan
Top achievements
Rank 1
Answers by
Thomas
Telerik team
Ryan
Top achievements
Rank 1
Ross Presser
Top achievements
Rank 1
Share this question
or