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

Linq implementation not handling nulls very well

3 Answers 87 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Michael Josiah
Top achievements
Rank 1
Michael Josiah asked on 12 May 2009, 02:06 PM
Basically I have the following linq query using OpenAccess

Dim products = (From pv In scope.Extent(Of DataModel.Productview)() _
                    Where pv.Dateadded = Date.Now _
                    Order By pv.Productviews Descending _
                    Select pv.Product).ToList

I have the equivilent using just Linq

Dim products = (From pv In db.productviews _
                     Where pv.Dateadded = Date.Now _
                     Order By pv.Productviews Descending _
                     Select pv.Product).ToList

I know that the database does not have any products that matches that criteria so I am expecting 0 in the product count. So if I try this

dim ProductCount as integer = products.count

This code will crash out using OpenAccess but I get the expected 0 count using just LINQ. Is there something I am missing out here? If I remove the where clause then it will work because there is data to retreive. What can I do to resolve this?

Also I have noticed that I can't do the following in OpenAccess

Dim products = (From pv In db.productviews _
                     Where pv.Dateadded = Date.Now.ToShortDateString _
                     Order By pv.Productviews Descending _
                     Select pv.Product).ToList

I am still debating whether to drop LINQ 2 SQL and go with OpenAccess but little problems like this really annoys me.Any help here would be appreciated.

3 Answers, 1 is accepted

Sort by
0
Thomas
Telerik team
answered on 13 May 2009, 08:28 AM
Hi Michael,

I've tried to reproduce this on my side, and it is working here. This is what I did:

    <TestMethod()> _ 
   Public Sub Count() 
        Dim x = (From c In MyBase.scope.Extent(Of Person)() Where c.Birthday = Date.Now Select c.Spouse).ToList() 
        Dim l = x.Count 
        Assert.AreEqual(0, l, "Number mismatch 1"
        Dim y = (From c In MyBase.scope.Extent(Of Person)() Where c.Birthday = Date.Now Select c.Spouse).Count() 
        Assert.AreEqual(0, y, "Number mismatch 2"
    End Sub 

As you can also see, it is not necessary to resolve the query to a list to get the number of elements, but you can also push it to a SQL count query (the second query does it).
When I added an Order By condition, I realized that ordering can currently only be done on simple fields, not reference fields. Is that the exception you've got? Of which type is Productview.Productviews ?

Best wishes,
Thomas
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Michael Josiah
Top achievements
Rank 1
answered on 13 May 2009, 08:47 AM
hmm

I am not sure about this. It just does not work for me. In my implementation productviews is an int field and im selecting the associated product object as the result (the productview table has an fk relation to the product table). However if the result returns 0 result objects I get an error with OpenAccess but a linq2sql will result with a 0 count rather than a null error.
0
Thomas
Telerik team
answered on 15 May 2009, 08:00 PM
Hi Michael,

can you give me the exact exception stack trace? I tried here to reproduce that but was unlucky.
I've transferred this ticket to support so that you can attach your app.config file too.

Kind regards,
Thomas
Tags
LINQ (LINQ specific questions)
Asked by
Michael Josiah
Top achievements
Rank 1
Answers by
Thomas
Telerik team
Michael Josiah
Top achievements
Rank 1
Share this question
or