简体   繁体   English

LINQ to Objects查询忽略层次结构中的空对象

[英]LINQ to Objects query ignoring null objects in hierarchy

I want to use LINQ-to-objects to query a collection of objects several levels deep, to extract a value. 我想使用LINQ-to-objects来查询几个级别深的对象集合,以提取值。 For example, given an Entity object I want to get the emailAddress string if a flag is set to 1, otherwise null: 例如,给定一个Entity对象,如果将标志设置为1,我想获取emailAddress字符串,否则为null:

 Entity.CommunicationCollection.Communication.CommunicationDetail.EmailAddress

Where CommunicationDetail looks like this: CommunicationDetail如下所示:

 public class CommunicationDetail
 {
     public int Flag;
     public string EmailAddress;
 }

The query I came up with looks like this: 我提出的查询如下所示:

 string email = Entity.CommunicationCollection.FirstOrDefault(x => x.Communication.CommunicationDetail.Flag == 1).EmailAddress;

The problem with this is that any null objects in the hierarchy will cause a null ref exception. 问题在于,层次结构中的任何空对象都将导致空引用异常。 Is there a way to structure the query to somehow ignore the nulls, without first checking every object? 有没有一种方法可以构造查询以某种方式忽略空值,而无需先检查每个对象? (The above is a simplified example, the nesting in the project I'm working on is much deeper.) (以上是一个简化的示例,我正在处理的项目中的嵌套要深得多。)

Well, you either have to check everything or use ?? 好吧,您要么必须检查所有内容,要么使用?? for defaulting, which would still be awkward. 默认设置,这仍然很尴尬。 I suspect you want something like: 我怀疑您想要类似的东西:

var email = Entity.CommunicationCollection
                  .Where(x => x.Communication != null &&
                              x.Communication.CommunicationDetail != null &&
                              x.Communication.CommunicationDetail.Flag == 1)
                  .Select(x => x.Communication.CommunicationDetail.EmailAddress)
                  .FirstOrDefault();

Note that email can still be null here... 请注意,此处的email仍然可以为null ...

Obviously if x.Communication can never be null, or x.Communication.CommunicationDetail can never be null, you can remove those checks - it's not clear what can be null in your model. 显然,如果x.Communication永远不能为null或x.Communication.CommunicationDetail永远不能为null,则可以删除这些检查-尚不清楚模型中什么可以为null。

I highly suggest creating a class with a generic method called something like IsNull, or IsNotNull and allowing it to take any any object type and checking the object to see if it is null. 我强烈建议使用称为IsNull或IsNotNull之类的通用方法创建一个类,并允许其采用任何对象类型并检查该对象是否为空。 That would allow you to not have to have separate validation for each object your are querying 这样一来,您不必对正在查询的每个对象进行单独的验证

public class NullChecker where T : class
{
    public static bool IsNotNull<T>(default(T) type)
    {
        return type != null;
    }
}

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

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