Strange exception using predicate

53 views
Skip to first unread message

sarc...@gmail.com

unread,
Jul 6, 2012, 11:59:35 AM7/6/12
to blto...@googlegroups.com
In the following code the third query throws "Object reference not set to an instance of an object."

The first case uses the count property of the Orders list successfully and return the expected results.
The second case uses a previously defined predicate to query the length property of a string member successfully.
In the third loop, when the two patterns are mixed to blows up when query3 is iterated.

        static void PredicateTest()
        {
            using (var db = new NorthwindDB())
            {
                var query1 = db.Employee.Where(p => p.Orders.Count < 2);

                foreach (var employee in query1)
                {
                    Console.WriteLine("{0} {1}", employee.EmployeeID, employee.FirstName);
                }

                Func<Employee, bool> fn2 = p => p.Address.Length > 10;
                var query2 = db.Employee.Where(fn2);

                foreach (var employee in query2)
                {
                    Console.WriteLine("{0} {1}", employee.EmployeeID, employee.FirstName);
                }

                Func<Employee, bool> fn3 = p => p.Orders.Count < 2;
                var query3 = db.Employee.Where(fn3);

                foreach (var employee in query3)
                {
                    Console.WriteLine("{0} {1}", employee.EmployeeID, employee.FirstName);
                }

            }
        }

sarc...@gmail.com

unread,
Jul 6, 2012, 12:19:48 PM7/6/12
to blto...@googlegroups.com
It looks like the association lists don't get initialized if there are no items in the relation.

Changing the predicate to the following avoids the exception

Func<Employee, bool> fn3 = p => p.Orders == null ? true : p.Orders.Count < 200;

Is the missing initialization of the lists intended behaviour or a bug?

Obasaa Esheetuu

unread,
May 30, 2013, 1:06:24 PM5/30/13
to blto...@googlegroups.com, sarc...@gmail.com
akkam jirtu yaa Oromoo
Reply all
Reply to author
Forward
0 new messages