Linq implementation not handling nulls very well

4 posts, 0 answers
  1. Michael Josiah
    Michael Josiah avatar
    92 posts
    Member since:
    Nov 2007

    Posted 12 May 2009 Link to this post

    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.
  2. Thomas
    Admin
    Thomas avatar
    590 posts

    Posted 13 May 2009 Link to this post

    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.
  3. DevCraft banner
  4. Michael Josiah
    Michael Josiah avatar
    92 posts
    Member since:
    Nov 2007

    Posted 13 May 2009 Link to this post

    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.
  5. Thomas
    Admin
    Thomas avatar
    590 posts

    Posted 15 May 2009 Link to this post

    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
Back to Top