简体   繁体   English

GetValue,如何将空值处理为Linq语句

[英]GetValue, How to handle null values into a Linq sentence

I'm having problems exporting an entity to excel. 我在将实体导出到Excel时遇到问题。

I have this Linq: 我有这个Linq:

List<PropertyInfo> props = FieldsToExport(EntitySet.AsQueryable());
int iCols = props.Count();

foreach (var Entity in EntitySet)
{
    var values = Enumerable.Range(0, iCols)
        .Select(ii => props[ii].GetValue(Entity)
                                .HandleNullProps()
        );

    // .... write in the sheet
} 

My problem is when the [ii] property of my entity has a null value, it is throwing a null exception even if HandleNullProps() is filtering it.. 我的问题是,当我实体的[ii]属性具有空值时,即使HandleNullProps()正在对其进行过滤,它也会引发空异常。

Here is my HandleNullProps() 这是我的HandleNullProps()

public static object HandleNullProps(this object s)
{
    if (s == null)
        return string.Empty;
    else
        return s;
    }
}

if props[ii] is null, then calling GetValue(Entity) will cause a NullReferenceException 如果props[ii]为null,则调用GetValue(Entity)将导致NullReferenceException

your handleNullProps method is not even reached - the exception is thrown before that 您甚至没有达到handleNullProps方法-在此之前引发异常

update your select to something like this 更新您的选择像这样

var values = Enumerable.Range(0, iCols).Select(ii => props[ii] != null ? props[ii].GetValue(Entity) : String.Empty)

and now your handleNullProps method becomes unnecessary. 现在您的handleNullProps方法变得不必要了。

Edit 编辑

Since you're trying Extension Methods, you could use them to get your values anyway like this 由于您正在尝试扩展方法,因此可以像这样使用它们来获取您的值

var values = Enumerable.Range(0, iCols).Select(ii => props[ii].GetValueOrDefault(Entity))

and let the method be defined like this 并像这样定义方法

public static object GetValueOrDefault(this object s, object entity)
{
    if (s == null)
        return string.Empty;
    else
        return s.GetValue(entity);
    }
}

keep in mind that I put object to both parameters because I don't know they proper types - set the correct types to both parameters and should work fine 请记住,我对两个参数都放置了object ,因为我不知道它们的正确类型-为两个参数设置正确的类型并且应该可以正常工作

the OrDefault part is inspired in one of the linq methods FirstOrDefault OrDefault部分是受linq方法之一FirstOrDefault启发的

It's not clear what is null in your case. 目前尚不清楚您的情况是什么。

If it's the Entity then you should filter it out before: 如果是Entity ,则应在以下条件之前将其过滤掉:

foreach (var Entity in EntitySet.Where(e=>e != null))

If it's the props[ii] then .Select(ii => props[ii]?.GetValue(Entity) should do it. 如果是props[ii].Select(ii => props[ii]?.GetValue(Entity)应该这样做。
And You can filter them out after wards the same way as before. 您也可以像以前一样在病房之后将其过滤掉。
Your code can become: 您的代码可以变成:

foreach (var Entity in EntitySet.Where(e => e != null))
{
    var values = Enumerable.Range(0, iCols)
        .Select(ii => props[ii]?.GetValue(Entity)).Where(v => v !=  null);

    // .... write in the sheet
} 

Update: if it's props[ii] then i suggest that you filter it beofre: 更新:如果是props[ii]那么我建议您先过滤它:

List<PropertyInfo> props = FieldsToExport(EntitySet.AsQueryable()).Where(p=>p!=null).ToList();

And the whole code can become simpler anyway: 无论如何,整个代码可以变得更简单:

var values = props.Select(p=>p.GetValue(Entity)).Where(v => v !=  null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM