在动态LINQ表达式空引用异常 [英] Null Reference Exception in a Dynamic LINQ Expression

查看:206
本文介绍了在动态LINQ表达式空引用异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用微软的动态的LINQ库/样品做排序名单。因此,例如,我有以下的C#代码:

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();



我在我的对象有一个0的情况:与另一个对象1的关系,它有一个属性可能显示在网格中。当我们试图和排序这一点,所以只要我所有的小工具有这个孩子正常工作。我们对 Child.Name 例如订购。当孩子为null然而,我们得到的空引用异常。

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.

我这里有一些选择,我知道我可以选择成为一个匿名类型并绑定到,我可以还揭露Child.Name父对象上,并通过代码处理这个问题(我不喜欢我的,包括这个对象模型)。

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?

修改

貌似我没有解释不够好。我使用的动态的LINQ库附带了 C#的样品的。这个库补充说,让你用一个lambda表达式的字符串就地所以我的代码实际上是这样的一些不错的扩展:

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.

推荐答案

如果我理解正确,我想你想要这样的:

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

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



LINQ就能生成SQL模仿这种表达。

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

这篇关于在动态LINQ表达式空引用异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆