Simple Projecting to String Fails Calling First (in VB)

Posts   
 
    
GabeNodland avatar
Posts: 65
Joined: 31-Dec-2004
# Posted on: 01-Oct-2008 06:19:17   

i believe i am on the latest ver from the 9-16


Function GetDomesticRowKeyByMaterialID(ByVal materialID As Integer, ByVal companyID As Integer) As String
    Dim q = From m In getMeta.Material _
            Where m.MaterialId = materialID _
            AndAlso m.Part.CompanyId = companyID _
            Select m.DomesticRowKey

    Return q.First
End Function

gives me this error:

**Unable to cast object of type 'System.Collections.Generic.List1[System.String]' to type 'System.String'.** at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.System.Linq.IQueryProvider.Execute[TResult](Expression expression) at System.Linq.Queryable.First[TSource](IQueryable1 source) at Labelmaster.DGDMS.llbl.ManagerClasses.MaterialManager.GetDomesticRowKeyByMaterialID(Int32 materialID, Int32 companyID) in C:\Projects\DGDMS\llbl\BusinessLayer\ManagerClasses\MaterialManager.vb:line 42 at show_InternationalDGA.Page_Load(Object sender, EventArgs e) in C:\WebSites\DGDMS\show\DGA.aspx.vb:line 28 at System.Web.UI.Control.OnLoad(EventArgs e) at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

the workaround appears to be

 Return q.ToArray.First

Is this a bug or a feature? or am i doing something wrong?

I can live with the toarray, just didn't seem quite right so i thought i would report it.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 01-Oct-2008 08:45:21   

This way should works fine too:

Function GetDomesticRowKeyByMaterialID(ByVal materialID As Integer, ByVal companyID As Integer) As String
    Dim q = From m In getMeta.Material _
            Where m.MaterialId = materialID _
            AndAlso m.Part.CompanyId = companyID _
            Select New With {.DomesticRowKey = m.DomesticRowKey}

    Return q.First().DomesticRowKey 
End Function
David Elizondo | LLBLGen Support Team
GabeNodland avatar
Posts: 65
Joined: 31-Dec-2004
# Posted on: 02-Oct-2008 06:04:52   

Thanks for the info, not as intuitive as it could be, but it works. wink

aspicer
User
Posts: 65
Joined: 14-Nov-2008
# Posted on: 04-Feb-2009 21:21:30   

Today I noticed this problem in C#. It would seem that .First() should evaluate the selected string without having to create an anonymous type or first call ToArray() then .First().

Is this going to make it to the bug list to be fixed in an upcoming release?

Thanks!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 05-Feb-2009 10:20:52   

aspicer wrote:

Today I noticed this problem in C#. It would seem that .First() should evaluate the selected string without having to create an anonymous type or first call ToArray() then .First().

Is this going to make it to the bug list to be fixed in an upcoming release?

Thanks!

I have no idea what you mean, sorry. 'First' means you want the first element. So the query will be made so you get the first element. I don't see what that has to do with .ToArray or other methods: if data is there, you get it, if not, you don't get the data.

'First' doesn't evaluate raw data coming from the db

Also, please be more specific: - runtime library buildnr - exact query which failed - exact exception - exact stacktrace.

Thanks.

Frans Bouma | Lead developer LLBLGen Pro
aspicer
User
Posts: 65
Joined: 14-Nov-2008
# Posted on: 05-Feb-2009 14:55:59   

My apologies Otis, I was meaning to only follow up on the original message in this thread.

We have the following statement:


string certFileName = (from cc in metaData.ClaimedCredit
                                       join c in metaData.Credit on cc.CreditId equals c.Id
                                       join cert in metaData.Certificate on c.CertificateId equals cert.Id
                                       where cc.Id == this.View.SelectedClaimedCreditId
                                       select cert.Filename).First();

That statement causes the exception Unable to cast object of type 'System.Collections.Generic.List1[System.String]' to type 'System.String'. at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.System.Linq.IQueryProvider.Execute[TResult](Expression expression) at System.Linq.Queryable.First[TSource](IQueryable1 source) ..., (just like the original message in the thread).

To overcome this runtime error, I have to do one of the suggestions in this thread: workaround 1:


string certFileName = (from cc in metaData.ClaimedCredit
                                       join c in metaData.Credit on cc.CreditId equals c.Id
                                       join cert in metaData.Certificate on c.CertificateId equals cert.Id
                                       where cc.Id == this.View.SelectedClaimedCreditId
                                       select new { cert.Filename}).First().Filename;

or workaround 2:


string certFileName = (from cc in metaData.ClaimedCredit
                                       join c in metaData.Credit on cc.CreditId equals c.Id
                                       join cert in metaData.Certificate on c.CertificateId equals cert.Id
                                       where cc.Id == this.View.SelectedClaimedCreditId
                                       select cert.Filename).ToArray().First();

Since I noticed this scenario was already posted on the forum, I was just wondering if a fix was on a to-do list for an upcoming release?

Version Info: SD.LLBLGen.Pro.LinqSupportClasses.NET35 = 2.6.08.1114 SD.LLBLGen.Pro.ORMSupportClasses.NET20 = 2.6.08.1013

Please let me know if you need more information.

Thanks!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 05-Feb-2009 15:37:04   

COuld you download the latest runtime libs from the customer area and try again? We had some bugfixes since that build date you're using.

Frans Bouma | Lead developer LLBLGen Pro
aspicer
User
Posts: 65
Joined: 14-Nov-2008
# Posted on: 16-Mar-2009 13:55:09   

Otis, Sorry it took so long, but this morning I download and reran the tests and I still have the same resulting exception.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 16-Mar-2009 14:10:55   

I think why this happens, string is an IEnumerable type. We fixed this last week, I'll attach the build to this post (as the problem and fix was reported in the Helpdesk forum)

Frans Bouma | Lead developer LLBLGen Pro
aspicer
User
Posts: 65
Joined: 14-Nov-2008
# Posted on: 16-Mar-2009 15:31:56   

It's fixed... THANKS!

GabeNodland avatar
Posts: 65
Joined: 31-Dec-2004
# Posted on: 21-Mar-2009 07:06:31   

Thanks for the fix, it works in VB too. I just needed to do this again today. Excellent timing!

Gabe