0

Below is the code in question. I receive Object reference not set to an instance of an object. on the where clause inside the Linq query. However, this only happens after it goes through and builds my viewpage.

Meaning: If I step through using debugger, I can watch it pull the correct order I am filtering for, go to the correct ViewPage, fill in the model/table with the correct filtered item, and THEN it comes back to my Controller and shows me the error.

public ActionResult OrderIndex(string searchBy, string search)
{
    var orders = repositoryOrder.GetOpenOrderList();

    if (Request.QueryString["FilterOrderNumber"] != null)
    {
        var ordersFiltered = from n in orders
            where n.OrderNumber.ToUpper().Contains(Request.QueryString["FilterOrderNumber"].ToUpper().ToString())
            select n;
        return View(ordersFiltered);
    }

    return View(orders);
}
1
  • 1
    where !string.IsNullOrEmpty(n.OrderNumber) && n.OrderNumber.ToUpper().Contains(Request.QueryString["FilterOrderNumber"].ToUpper().ToString()) Commented Mar 12, 2014 at 21:23

3 Answers 3

2

its always better to manipulate your strings and other things outside the linq query ,

please refer : http://msdn.microsoft.com/en-us/library/bb738550.aspx

from the readability point of view also its not good ,

public ActionResult OrderIndex(string searchBy, string search)
{
    var orders = repositoryOrder.GetOpenOrderList();
    var orderNumber = Request.QueryString["FilterOrderNumber"];
    if (!string.IsNullOrEmpty(orderNumber))
    {
        orderNumber = orderNumber.ToUpper();
        var ordersFiltered = from n in orders
            where n.OrderNumber.ToUpper().Contains(orderNumber)
            select n;
        return View(ordersFiltered);
    }

    return View(orders);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Your query is not being executed in your Action method because you don't have a ToList (or equivalent) added to your query. When your code returns, your query will be enumerated somewhere in your view and that's the point where the error occurs.

Try adding ToList to your query like this to force query execution in your action method:

var ordersFiltered = (from n in orders
                     where n.OrderNumber.ToUpper().Contains(Request.QueryString["FilterOrderNumber"].ToUpper().ToString())
                     select n).ToList();

What's going wrong is that a part of your where clause is null. This could be your query string parameter. Try moving the Request.QueryString part out of your query and into a temporary variable. If that's not the case make sure that your orders have an OrderNumber.

Comments

0

You both were right. Just separately.

This fixed my problem

var ordersFiltered = (from n in orders
                      where !string.IsNullOrEmpty(n.OrderNumber) && n.OrderNumber.ToUpper().Contains(Request.QueryString["FilterOrderNumber"].ToUpper().ToString())
                      select n);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.