4

I have a 2 stage query join that is failing on the Contains operator. The error I'm getting is Object Reference not set to an instance of an object. I'm creating dynamic queries up front, and executing in the join.

This works: AQuery.Where("AssigneeName == \"Michael Jackson\"")

This fails with "object reference not set to an instance of an object": AQuery.Where("AssigneeName.Contains(\"Michael Jackson\")")

Here's my code:

        using (ReqEntitiesDataContext dc = new ReqEntitiesDataContext(SPContext.Current.Web.Url))
        {

            // LINQ Deferred Query Execution
            var AQuery = from a in dc.Assignees select a;
            if (assigneeQuery.Count > 0)
            {
                AQuery = from a in AQuery.Where(string.Join(" ", assigneeQuery.ToArray())) select a;
            }

            var RQuery = from r in dc.ReqLibrary select r;
            if (requestQuery.Count > 0)
            {
                RQuery = from r in RQuery.Where(string.Join(" ", requestQuery.ToArray())) select r;
            }


            // LINQ 2 Stage Query Execution Join
            var resultQuery = from a in AQuery.ToList() 
                              join r in RQuery on a.Title equals r.RequestID
                              orderby r.RequestID ascending
                              select new RequestType
                              {
                                  RequestID = r.RequestID,
                                  ReceivedDate = r.ReceivedDate.Value,
                                  RequestType = r.RequestType,                                 
                                  Assignee = a.AssigneeName,
                                  AssigneeSection = a.AssigneeSection,
                                  AssigneeDivision = a.AssigneeDivision,
                                  RequestStatus = r.RequestStatus
                              };

            CreateTableResults(resultQuery);
        }
Casey
  • 43
  • 1
  • 1
  • 5

3 Answers3

16

You need to handle the case where AssigneeName is null.

Jon La Marr
  • 1,358
  • 11
  • 14
  • 2
    ok, that worked, adding (AssigneeName != NULL and AssigneeName.Contains(\"Michael Jackson\")") – Casey Aug 22 '13 at 18:58
5

First, make sure the username is not empty. Then search for it

 var result = _users.Where(x => !string.IsNullOrEmpty(x.UserName) && x.UserName.Contains(userName)).ToList();
Mohammad Fazeli
  • 648
  • 10
  • 13
0

I had a similar issue, but I wasn't attempting to filter any of the output - it turned out that every mapped field in the model had "Entity Key" set to true.

i.e. The following was throwing "object reference not set to an instance of an object" errors everywhere.

var x = entities.Users
        .AsEnumerable()
        .Select(x => x)
        .ToList();

Simply opening the Model's EDMX file, selecting the necessary object, clicking each property and then setting the Entity Key property to False resolved this for me. :)

XtraSimplicity
  • 5,704
  • 1
  • 28
  • 28