表达式Lambda命令具有空值的字段 [英] Expression Lambda to order fields with null value

查看:267
本文介绍了表达式Lambda命令具有空值的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 为每个排序在MesDonnees.MesOptions 
如果Sort.Sens < 2然后
Dim sortPropertyName As String = Sort.ColName

If(TypeClass.GetProperties()。Any(Function(prop)prop.Name = sortPropertyName AndAlso prop.CanRead))然后

'信息sur lapropriétérecherchée
Dim pinfo As PropertyInfo = TypeClass.GetProperty(sortPropertyName)

Dim Expr As Expression = Expression.Property(paramExpr,pinfo)
Dim Tostr As Expression = Expression.Call(Expr,ToString,Nothing)

Dim orderByFunc As Func(Of MaClass,Object)= Expression.Lambda(Of Func(Of MaClass,Object) )(Tostr,paramExpr).Compile()
Dim sortFunc As Func(OfOnableable(Of MaClass))IOrderedEnumerable(Of MaClass))= Nothing

如果(不是CBool ))然后
sortFunc =(Function(source)source.OrderBy(orderByFunc))
Else
sortFunc =(Function(source)source.OrderByDescending(o rderByFunc))
End If

query = sortFunc(query).ToList()

如果
结束If
Next

Sort.ColName 是我的字段I的名称想要过滤。



当我没有空值时,这是完美的,但是当我有一个空值时,我会在查询中找到一个例外= sortFunc(query).ToList()


对象引用未设置为对象的实例


我看到不同的代码与 Expression.Call ISNullOrEmpty 但我不知道在我的情况下如何正确使用它。感谢您的帮助和解释

我想要空或空在第一个像。



解决方案

您可以将 toStr 表达式更改为包含 Coalesce 操作,其中正确的表达式是常量。请注意,左表达式需要是引用类型(或可空值类型),因此您还需要确保属性类型不是值类型。



这是一个例如,应该工作:

  Dim toStr As Expression = Expression.Call(
If(pinfo.PropertyType.IsValueType, expr,Expression.Coalesce(expr,Expression.Constant(String.Empty))),
ToString,
没有

pre>

I use a dynamic method to order a list of object.

For Each Sort In MesDonnees.MesOptions
    If Sort.Sens < 2 Then
        Dim sortPropertyName As String = Sort.ColName

        If (TypeClass.GetProperties().Any(Function(prop) prop.Name = sortPropertyName AndAlso prop.CanRead)) Then

            'Information sur la propriété recherchée
            Dim pinfo As PropertyInfo = TypeClass.GetProperty(sortPropertyName)

            Dim Expr As Expression = Expression.Property(paramExpr, pinfo)
            Dim Tostr As Expression = Expression.Call(Expr, "ToString", Nothing)

            Dim orderByFunc As Func(Of MaClass, Object) = Expression.Lambda(Of Func(Of MaClass, Object))(Tostr, paramExpr).Compile()
            Dim sortFunc As Func(Of IEnumerable(Of MaClass), IOrderedEnumerable(Of MaClass)) = Nothing

            If (Not CBool(Sort.Sens)) Then
                sortFunc = (Function(source) source.OrderBy(orderByFunc))
            Else
                sortFunc = (Function(source) source.OrderByDescending(orderByFunc))
            End If

            query = sortFunc(query).ToList()

        End If
    End If
Next

Sort.ColName is the name of my field I want to filter.

When I have no null value, it's perfect but when I have a null value, I get an exception in the line query = sortFunc(query).ToList() :

Object reference not set to an instance of an object

I see different code with Expression.Call and ISNullOrEmpty but I don't know how use it correctly in my case. I want null or Empty are in first like a "".

Thanks for your help and explanation

解决方案

You can change your toStr expression to include a Coalesce operation where the right expression is a constant. Note that the left expression needs to be reference type (or a nullable value type) so you also need to ensure that the property type is not a value type.

Here's an example that should work:

Dim toStr As Expression = Expression.Call(
    If(pinfo.PropertyType.IsValueType, expr, Expression.Coalesce(expr, Expression.Constant(String.Empty))),
    "ToString",
    Nothing
)

这篇关于表达式Lambda命令具有空值的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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