Linq Query

Posts   
 
    
Anthony
User
Posts: 155
Joined: 04-Oct-2006
# Posted on: 05-May-2009 12:40:41   

vb.net,vs2008, adapter, llblgen 2.6

    Using adapter As New DataAccessAdapter()
        Dim metaData As New LinqMetaData(adapter)
        Dim q = (From c In metaData.State Where c.CountryId = oTrip.FromCountryId And c.State = RadComboBoxFromCountry.Text _
                Select c)
        If q.count > 0 Then '*****Get error if q returns no results*****
            oTrip.FromStateId = q(0).StateId
        Else

        End If

        adapter.SaveEntity(oTrip)
    End Using

How do i fix the code above please?

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

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 05-May-2009 20:59:45   

Try changing the "And" in the following line to "AndAlso"

 Dim q = (From c In metaData.State Where c.CountryId = oTrip.FromCountryId And c.State = RadComboBoxFromCountry.Text _
                    Select c)

Matt

Anthony
User
Posts: 155
Joined: 04-Oct-2006
# Posted on: 06-May-2009 02:20:08   

the above does not resolve the issue..thanks anyway

Anthony
User
Posts: 155
Joined: 04-Oct-2006
# Posted on: 06-May-2009 03:45:25   

got a step further by changing

...Where c.CountryId = oTrip.FromCountryId.Value (.Value resolved the issue)

but now geting error

An exception was caught during the execution of a retrieval query: An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Add a name or single space as the alias name. . Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception.

When i profile the statement i get this..

exec sp_executesql N'SELECT DISTINCT TOP 1 COUNT(*) AS [LPAV_] FROM [MyWay].[dbo].[State] [LPLA_1] WHERE ( ( ( ( ( [].[LPFA_2] = @LPFA_21)))))',N'@LPFA_21 bit',@LPFA_21=1

Any suggestion..look like a bug i think.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 06-May-2009 07:37:00   

Please update to the latest LLBLGen runtime library version and try again. Also please post the final code you are using.

David Elizondo | LLBLGen Support Team
Anthony
User
Posts: 155
Joined: 04-Oct-2006
# Posted on: 06-May-2009 09:26:29   

UPDATED TO LATEST VERSION

the sql being produced is now..but same error

exec sp_executesql N'SELECT DISTINCT TOP 1 COUNT(*) AS [LPAV_] FROM (SELECT [LPLA_1].[StateId], [LPLA_1].[State] FROM [YouGoingMyWay].[dbo].[State] [LPLA_1] WHERE ( ( ( [].[LPFA_2] = @LPFA_21)))) [LPA_L1]',N'@LPFA_21 bit',@LPFA_21=1

CODE.....

Dim oTrip As New TripEntity

    Using adapter As New DataAccessAdapter()
        oTrip.FromCountryId = Me.RadComboBoxFromCountry.SelectedValue
        Dim metaData As New LinqMetaData(adapter)
        Dim sState As String = RadComboBoxFromState.Text

        Dim q = (From c In metaData.State Where c.CountryId = oTrip.FromCountryId.Value And c.State = sState _
                Select c.StateId, c.State)


        If q.Count > 0 Then
            oTrip.FromStateId = q(0).StateId
        Else
            Dim oState As New StateEntity
            oState.State = RadComboBoxFromState.Text
            oState.CountryId = oTrip.FromCountryId
            adapter.SaveEntity(oState, True)
            oTrip.FromStateId = oState.StateId
        End If

        oTrip.Title = Me.TextBoxTitle.Text
        oTrip.Description = Me.TextBoxDescription.Text
        oTrip.OwnerUserId = 21
        adapter.SaveEntity(oTrip)
    End Using
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39859
Joined: 17-Aug-2003
# Posted on: 06-May-2009 09:53:54   

Use DQE tracing to obtain the query produced. The SQL you post doesn't match the query you ran. I can't reproduce it in C#, I'll now try in VB.NET. However keep in mind that we don't post threads like: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=12769 for nothing.

Btw, using .Value on 1 side and not on the other side (while both are nullable(Of T) makes the expression tree become unequal, as types don't match necessarily. For reasons unknown, this does compile but deeper in the linq provider this causes problems.

I tried:


[Test]
public void CountOverSimpleQueryWithNullableFieldComparison()
{
    using(DataAccessAdapter adapter = new DataAccessAdapter())
    {
        LinqMetaData metaData = new LinqMetaData(adapter);
        OrderEntity ot = new OrderEntity();
        ot.EmployeeId = 4;
        string cid = "ALFKI";

        var q = from o in metaData.Order
                where o.EmployeeId == ot.EmployeeId && o.CustomerId == cid
                select new { o.CustomerId, o.OrderId };
        int count = q.Count();
        Assert.AreEqual(2, count);
    }
}

and


[Test]
public void CountOverSimpleQueryWithNullableFieldComparison()
{
    using(DataAccessAdapter adapter = new DataAccessAdapter())
    {
        LinqMetaData metaData = new LinqMetaData(adapter);
        OrderEntity ot = new OrderEntity();
        ot.EmployeeId = 4;
        string cid = "ALFKI";

        var q = from o in metaData.Order
                where o.EmployeeId.Value == ot.EmployeeId.Value && o.CustomerId == cid
                select new { o.CustomerId, o.OrderId };
        int count = q.Count();
        Assert.AreEqual(2, count);
    }
}

Both worked. o.EmployeeId is nullable.

Will now try VB.NET

Frans Bouma | Lead developer LLBLGen Pro
Anthony
User
Posts: 155
Joined: 04-Oct-2006
# Posted on: 06-May-2009 09:59:55   

thank you

how do i use the Use DQE tracing?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39859
Joined: 17-Aug-2003
# Posted on: 06-May-2009 10:01:46   

<Test()> _
    Public Sub CountOverSimpleQueryWithNullableFieldComparison()
        Using adapter As New DataAccessAdapter()
            Dim metaData As New LinqMetaData(adapter)
            Dim ot As New OrderEntity()
            ot.EmployeeId = 4
            Dim cid As String = "ALFKI"

            Dim q = From o In metaData.Order _
              Where o.EmployeeId = ot.EmployeeId AndAlso o.CustomerId = cid _
              Select o.CustomerId, o.OrderId
            Dim count As Integer = q.Count()
            Assert.AreEqual(2, count)
        End Using
    End Sub

Indeed gives a lame error, which is very strange as the expression tree should be the same, right? (lovely Microsoft, they of course didn't provide any info on these kind of problems.)

though when I do:


<Test()> _
    Public Sub CountOverSimpleQueryWithNullableFieldComparison()
        Using adapter As New DataAccessAdapter()
            Dim metaData As New LinqMetaData(adapter)
            Dim ot As New OrderEntity()
            ot.EmployeeId = 4
            Dim cid As String = "ALFKI"

            Dim q = From o In metaData.Order _
              Where o.EmployeeId.Value = ot.EmployeeId.Value AndAlso o.CustomerId = cid _
              Select o.CustomerId, o.OrderId
            Dim count As Integer = q.Count()
            Assert.AreEqual(2, count)
        End Using
    End Sub

(mind the .Value on both sides!) it works. Albeit in a less efficient query than in C# thanks to the VB.NET compiler people who think that querying a database should be the same as querying an in-memory object graph which obeys the insane VB.NET nullability comparison rules. disappointed )

Will look into making this less problematic. You should use .Value on both sides to make this work.

DQE tracing

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39859
Joined: 17-Aug-2003
# Posted on: 06-May-2009 11:17:43   

Fixed it. Was a matter of fiddling with nullable types to get it working. The main issue is that one side results in a constant which of course isn't a nullable, which is then converted to a nullable element, causing the problem.

See attached dll for the working version.

Frans Bouma | Lead developer LLBLGen Pro
Anthony
User
Posts: 155
Joined: 04-Oct-2006
# Posted on: 06-May-2009 14:23:14   

appreciate your quick response and assistance. You bug fix worked.

thanks