user646306
user646306

Reputation: 513

LINQ/LinqPad: same query different results

So we copy and paste the exact same query from LinqPad into our EF 4.3 application, pointed at the exact same database and get a different result. In LinqPad we get 2 records returned. In our application we reaise an error "Object reference not set to an instance of an object."

        var Shippings = shippingRepository.All.ToArray();
        var SalesOrderHeaders = salesOrderHeaderRepository.All.ToArray();
        var Customers = customerRepository.All.ToArray();
        var Stores = storeRepository.All.ToArray();
        var Departments = departmentRepository.All.ToArray();

        var toShip = from sh in Shippings
                     join h in SalesOrderHeaders on sh.OrderId equals h.SalesOrderHeaderId
                     join c in Customers on h.CustomerId equals c.CustomerId
                     join st in Stores on h.StoreId equals st.StoreId
                     join d in Departments on h.DepartmentId equals d.DepartmentId into outer
                     from o in outer.DefaultIfEmpty()
                     select new 
                     {
                        OrderId = sh.OrderId,
                        CustomerName = c.Name,
                        StoreName = st.Name,
                        DepartmentName = (o.Name == null) ? o.Name : "None",
                        DeliveryDate = h.DeliveryDateTime
                     };

In the application code, when we remove the outer join (to add Departments) and it's associated field the query returns the same 2 records asn in LinqPad.

Does anyone have any insight into how to fix this feature?

Upvotes: 1

Views: 1039

Answers (2)

Muhammad Adeel Zahid
Muhammad Adeel Zahid

Reputation: 17794

Click on "Add a connection" in linqpad and select datacontext from assembly like enter image description here
You can choose Entity Framework datacontext or Entity Framework BDContext with POCO depending upon your scenario. click next and provide path to the assembly along with connection string and you will be good to go.

Upvotes: 3

Narthring
Narthring

Reputation: 1419

In LINQPad are you actually querying against your entity model? Take a look at this link if you aren't. I had a similar problem when starting out and didn't realize I had set up a default LINQ to SQL connection earlier and was querying against that.

Upvotes: 0

Related Questions