OT: Linq oddness

OT: Linq oddness

Old forum URL: forums.lhotka.net/forums/t/4460.aspx


ajj3085 posted on Wednesday, March 05, 2008

Hi,

I have a wierd problem with Linq.  I have the following code in my fetch method:

        /// <summary>Loads the list.</summary>
        /// <param name="documentHistoryId">The document
        /// for which to get items.</param>
        /// <param name="db">The connection to use.</param>
        /// <param name="taxRate">The tax rate items
        /// should use when loading.</param>
        private void LoadSelf(
            int documentHistoryId,
            decimal? taxRate,
            MedDataDataContext db
        ) {
            var items =
                from lineitems in db.LineItemHistory
                where lineitems.DocumentHistoryId == documentHistoryId &&
                    lineitems.ParentLineItemId == null
                orderby lineitems.Position
                select lineitems;

            IsReadOnly = false;
            foreach ( var item in items ) {
                LineItemRevision lineRev = LineItemRevision.GetItem( item, 0, taxRate, db );
                Add( lineRev );

                if ( LoadChildren(
                    documentHistoryId,
                    item.LineItemId.Value,
                    0,
                    taxRate,
                    db
                ) ) {
                    lineRev.IsGroupStart = true;
                }
            }
            IsReadOnly = true;
        }

        /// <summary>Loads the list.</summary>
        /// <param name="documentHistoryId">The document
        /// for which to get items.</param>
        /// <param name="db">The connection to use.</param>
        /// <param name="taxRate">The tax rate items
        /// should use when loading.</param>
        /// <param name="parentLineItemId">The id of
        /// the parent for items to load.</param>
        /// <param name="depth">The depth of the items
        /// being loaded.</param>
        private bool LoadChildren(
            int documentHistoryId,
            int parentLineItemId,
            int depth,
            decimal? taxRate,
            MedDataDataContext db
        ) {
            int childCount;

            var items =
                from lineitems in db.LineItemHistory
                where lineitems.DocumentHistoryId == documentHistoryId &&
                    lineitems.ParentLineItemId == parentLineItemId
                orderby lineitems.Position
                select lineitems;

            childCount = 0;
            depth += 1;

            foreach ( var item in items ) {
                LineItemRevision lineRev;

                childCount += 1;

                lineRev = LineItemRevision.GetItem( item, depth, taxRate, db );
                Add( lineRev );

                if ( LoadChildren(
                    documentHistoryId,
                    item.LineItemId.Value,
                    depth,
                    taxRate,
                    db
                ) ) {
                    lineRev.IsGroupStart = true;
                }
            }

            return childCount > 0;
        }

So, you notice that the queries are identical in both LoadSelf and LoadChildren.  It seems natural that I make LoadSelf simplier by changing it to this:

        private void LoadSelf(
            int documentHistoryId,
            decimal? taxRate,
            MedDataDataContext db
        ) {
            IsReadOnly = false;
            LoadChildren(
                    documentHistoryId,
                    null
                    -1,
                    taxRate,
                    db
                );
            IsReadOnly = true;
        }

And I make LoadChildren take a Int32? instead of just an Int32.  For some reason, making these chagnes causes linq to mess up the query.  Instead of specifying ParentLineItemId IS NULL, it tries ParentLineItemId = Null, which of course never works.

Any ideas?

rasupit replied on Thursday, March 06, 2008

Can you try with :
parentLineItemId==null? lineitems.ParentLineItemId == null :lineitems.ParentLineItemId == parentLineItemId

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1967216&SiteID=1

ajj3085 replied on Thursday, March 06, 2008

Ugh.  I'll leave it as is, since that results in a case statement within the sql.  I don't buy the answer in that post; its a bug, it shouldn't matter if its hardcoded null or null from a variable.

Copyright (c) Marimer LLC