Linq and VB general frustration

Posts   
 
    
GabeNodland avatar
Posts: 65
Joined: 31-Dec-2004
# Posted on: 17-Sep-2008 07:23:18   

Hello,

I am sorry to bother you, but I can not find any documentation on how to do this simple linq query. It seems I think I found the hack to get nullable integers to compare, but I can't figure out IS NULL. My linq ship is sinking and i think I am going to have to start bailing water with a RelationPredicateBucket.

I know the place to look is in the unit tests, are there VB versions that we can download somewhere, it seems there are a lot of differences in how these languages work with LLBL Linq?

Anyhow, I am trying to do a simple linq query and I keep hitting dead ends.

--The table CREATE TABLE [dbo].[Document]( [DocumentID] [int] IDENTITY(1,1) NOT NULL, --PK [CompanyID] [int] NOT NULL, [PartID] [int] NULL, --If PartID is NULL then document belongs to entire company [DocumentName] [varchar(30)] NULL )

--HERE is the Query I am trying to do will in linq.

select * from Document Where PartID=5 or (CompanyID=3 and PartID is NULL)

'the linq Dim q = From d In meta.Document Where _ CInt(d.PartId) = part.PartId OrElse _ (d.CompanyId.Equals(part.CompanyId) AndAlso d.PartId.HasValue = True) _ Select d

My error. !!No known database function mapping found for property 'HasValue', there must be a way to do this.

please help

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 17-Sep-2008 09:22:37   

Please try this instead:

d.PartId <> Nothing

Also, read at this manual page the important LINQ note for VB Users (the second Note box).

David Elizondo | LLBLGen Support Team
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 17-Sep-2008 09:37:47   

And also: be sure you run the latest build of the linq provider. The VB.NET compiler produces different expression trees than the C# compiler. These differences are unknown, so it's a trial / error world, as MS hasn't documented any of this. We have fixed a lot of the vb.net specific issues (like this one) in previous months, please check if you're running the latest build and if so, we'll fix this issue too.

Frans Bouma | Lead developer LLBLGen Pro
GabeNodland avatar
Posts: 65
Joined: 31-Dec-2004
# Posted on: 17-Sep-2008 21:34:44   

I am on the latest version here are some queries and the results, I will do things the old way for now.

Dim q1 = From d In meta.Document Where d.PartId = Nothing Select d q1.ToArray()

The binary operator Equal is not defined for the types 'System.Int32' and 'System.Nullable`1[System.Int32]'.

Dim q2 = From d In meta.Document Where d.PartId <> Nothing Select d q2.ToArray()

The binary operator NotEqual is not defined for the types 'System.Int32' and 'System.Nullable`1[System.Int32]'.

Dim q3 = From d In meta.Document Where d.PartId = 5 Select d q3.ToArray()

The binary operator Equal is not defined for the types 'System.Int32' and 'System.Nullable`1[System.Int32]'.

Dim q4 = From d In meta.Document Where d.PartId.Value = 5 Select d q4.ToArray()

No known database function mapping found for property 'Value'

Dim q5a = From d In meta.Document Where d.CompanyId = 5 Select d q5a.ToArray()

Works

Dim q6 = From d In meta.Document Where CInt(d.PartId) = 2 Select d q6.ToArray()

Works

Dim q7 = From d In meta.Document Where d.PartId.Value.Equals(5) Select d q7.ToArray()

No known database function mapping found for property 'Value'

Dim q8 = From d In meta.Document Where d.PartId.Equals(Nothing) Select d q8.ToArray()

The binary operator Equal is not defined for the types 'System.Int32' and 'System.Object'.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 18-Sep-2008 07:59:10   

Can't reproduce your errors with the latest build. Check this discussion: http://llblgen.com/TinyForum/Messages.aspx?ThreadID=13966

David Elizondo | LLBLGen Support Team
GabeNodland avatar
Posts: 65
Joined: 31-Dec-2004
# Posted on: 18-Sep-2008 18:08:39   

I deleted all my obj and bin directories and rebuilt that seemed to get me working.

I found this to be strange, but found another way to make it work.

Dim q2 = From d In meta.Document Where _ d.CompanyId = part.CompanyId AndAlso d.PartId <> Nothing _ Select d

The binary operator Equal is not defined for the types 'System.Nullable`1[System.Boolean]' and 'System.Boolean'.

however this works

Dim q2 = From d In meta.Document Where _ d.PartId <> Nothing _ Select d

and this works

Dim q2 = From d In meta.Document Where _ d.CompanyId = part.CompanyId _ Select d

so does this

Dim q2 = From d In meta.Document Where _ d.CompanyId = part.CompanyId AndAlso d.PartId.HasValue _ Select d

thanks,

Gabe

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 18-Sep-2008 20:34:09   

GabeNodland wrote:

I deleted all my obj and bin directories and rebuilt that seemed to get me working.

I found this to be strange, but found another way to make it work.

Dim q2 = From d In meta.Document Where _ d.CompanyId = part.CompanyId AndAlso d.PartId <> Nothing _ Select d

The binary operator Equal is not defined for the types 'System.Nullable`1[System.Boolean]' and 'System.Boolean'.

d.PartId <> Nothing

this suggests PartId is a Nullable(Of T), correct? If so, then why do you ignore advice that's both in the manual and linked above as well? simple_smile Let me re-quote that:

Also, read at this manual page the important LINQ note for VB Users (the second Note box).

Gabe, it sometimes might work, and sometimes it won't, but what always works is this: EITHER: a) d.PartId.HasValue or b) d.PartId.Value = somevalue

The reason why is discussed in the link above which was also posted before by David in this thread. We're sorry it's this way, but we can't do a thing about this, we didn't write the VB.NET compiler nor are we sitting in a building next to the VB.NET compiler team so we can't ask when what is emitted and what to look for when nullable types etc. are in the query. To avoid problems, please follow the advice in the section in the manual. Thanks.

Frans Bouma | Lead developer LLBLGen Pro
GabeNodland avatar
Posts: 65
Joined: 31-Dec-2004
# Posted on: 18-Sep-2008 22:44:16   

Frans,

I have no problem using HasValue and Value.

I was just pointing out my results in case you didn't know. It is quite clear to me now from your last post that you already know about this issue.

however <> Nothing is what was suggested to me instead of hasvalue and the note in the manual suggests that =Nothing should be used, no mention of hasvalue.

simple_smile to quote the manual:

To filter on Nothing (null), it's necessary to use the field instead of the '.Value' property, though this is ok, as the VB.NET compiler doesn't wrap these expressions with a coalesce statement:

Dim q = From o In metaData.Order _ Where o.EmployeeId = Nothing _ Select o

I am trying my best to keep up, and I sincerly appreciate all of the help I get in the forum. I try to carefully look at everything recommended.

After getting this working I am quite happy and a lot less frustrated.

Thanks,

Gabe

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 19-Sep-2008 07:02:11   

Great the thinks are working Gabe wink Thanks for the feedback.

David Elizondo | LLBLGen Support Team