Null Reference Exception in a Dynamic LINQ Expression
Asked Answered
A

4

6

I am using the Dynamic Linq Library / Sample from Microsoft to do ordering on a list. So for example I have the following C# code:

   myGrid.DataSource=repository.GetWidgetList()
         .OrderBy(sortField + " " + sortDirection).ToList();

I have a case where my Object have a 0:1 relationship with another object, which has a property that might be displayed in the grid. When we try and sort this, it works fine so long as all my Widgets have this child. We are ordering by Child.Name for example. When Child is null however, we get the null reference exception.

I have some options here which I know I could select into an anonymous type and bind to that, I could also expose the Child.Name on the parent object and handle this via code (Which I don't like comprising my object model for this).

In an ideal world I'd like to update the library to handle this case. Before I dive into it, I'm wondering if anyone has ran across this or not and has a solution already?

Edit

Looks like I didn't explain well enough. I am using the Dynamic Linq Library which comes with the C# samples. This library adds some nice extensions that let you use a string inplace of a lambda expression So my code is actually something like this:

private  void BindGrid(sortField,sortDirection)
{

     this.grid.DataSource=....OrderBy("MyField ASC")....
}

Of course the string there is replaced with the parameters. But this allows us to change the sorting dynamically as the user clicks on a grid header. We don't have to if then else logic to handle all the permutations.

My solution as I documented bellow changes my nice clean method into:

private void BindGrid()
{
   var sortField=this._sortField;
   if (sortField=="Child.Name")
   {
       sortField="iif(Child==null,null,Child.Name)";
   }
   this.grid.DataSource=repository.GetWidgetList()
                                  .OrderBy(sortField + " " + this._sortDirection)
                                  .ToList();
}

And while this works, this now means I have to update this code as we add new fields or properties which we want to expose in the grid which are on a child object.

Arundell answered 18/9, 2009 at 14:18 Comment(0)
M
4

If I understand you correctly, I think you want this:

repository.GetParentObjects()
    .OrderBy(p => p.Child == null ? "" : p.Child.Name);

LINQ will be able to generate SQL that mimics this expression.

Metamorphic answered 18/9, 2009 at 14:49 Comment(2)
Nope this is not what I am referring to. The dynamic linq is part of the C# sample code and lets you use a string as an OrderBy for example. It compiles the string to a lambda expression at run time.Arundell
Why was this answer accepted as the actual answer? I'm in the same situation.Oxymoron
A
2

On solution I've found which in my case is not ideal again would be to detect when the expression is going to access the child, to change the sort expression to be

iif(Child == null,null,Child.Name) ASC

Ideally this logic can be baked into the dynamic library, I'd rather not have to modify each grid all over the place to handle all the cases this will impact.

Arundell answered 18/9, 2009 at 14:30 Comment(0)
G
1

I had the same issue but the best solution I found was to make your code a little more generic by changing it into this:

private void BindGrid()
{
    var sortField = this._sortField;
    var splitted_sortField = this._sortField.Split(new char[]{'.'}, StringSplitOptions.RemoveEmptyEntries);
    if (splitted_sortField.Length > 1)
    {
        sortField = "iif("+splitted_sortField[0]+"==null,null,"+sortField+")";
    }
    this.grid.DataSource = repository.GetWidgetList()
                                     .OrderBy(sortField + " " + this._sortDirection)
                                     .ToList();
}

Not perfect, won't like give access to childs of childs, but saves u from updating your code every time u get a new nullable child.

Gold answered 17/2, 2012 at 23:27 Comment(0)
C
-2

I don't really understand the problem (maybe because it is friday evening here already...), but can't you sort the list like this:

   myGrid.DataSource=repository.GetWidgetList()
     .OrderBy(w => w.SortField).ToList();

where SortField is the property you want to sort on. This should work even when the value is null...

Sorry if it's maybe completely beside the point...

Claiborn answered 18/9, 2009 at 14:45 Comment(1)
You completly missed the point. Since your not the only I must have not explained myself well enough. I updated the question so it is probally a little but more clear.Arundell

© 2022 - 2024 — McMap. All rights reserved.