Enumerable.FirstOrDefault 方法是否可以处理 null 参数

发布于 2024-11-07 02:55:45 字数 864 浏览 0 评论 0原文

是否可以编写此代码,以便在与父属性为 null 的对象 (x) 进行比较时返回列表中父属性为 null 的项目?

MyObject obj = objList.FirstOrDefault(o => n.Parent.Equals(x.Parent));

假设“Equals”方法被正确覆盖,如果“objList”中存在一个父级为空的项目,并且“对象引用未设置为对象的实例”,则此方法将失败。例外。

我假设会发生这种情况,因为如果 n.Parent 为 null,则无法调用其 Equal 方法。

无论如何,我目前采用了这种方法:

MyObject obj = null;
foreach (MyObject existingObj in objList)
{
    bool match = false;

    if (x.Parent == null)
    {
        if (existingObj.Parent == null)
        {
            match = true;
        }
    }
    else
    {
        if (existingObj.Parent != null)
        {
            if (x.Parent.Equals(existingObj.Parent))
            {
                match = true;
            }
        }
    }

    if (match)
    {
        obj= existingObj;
        break;
    }

因此,虽然它确实有效,但它不是很优雅。

Can this code be written so that items in the list with a parent property of null will be returned when comparing to an object (x) that also has a null Parent?

MyObject obj = objList.FirstOrDefault(o => n.Parent.Equals(x.Parent));

Assuming the "Equals" method is correctly overridden, this fails where there is an item in the "objList" with a null Parent - with an "Object reference not set to an instance of an object." exception.

I would assume that occurs because if n.Parent is null, you can't call its Equal method.

Anyway, I currently resorted this this approach:

MyObject obj = null;
foreach (MyObject existingObj in objList)
{
    bool match = false;

    if (x.Parent == null)
    {
        if (existingObj.Parent == null)
        {
            match = true;
        }
    }
    else
    {
        if (existingObj.Parent != null)
        {
            if (x.Parent.Equals(existingObj.Parent))
            {
                match = true;
            }
        }
    }

    if (match)
    {
        obj= existingObj;
        break;
    }

So while it does work, it's not very elegant.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩起发的微风 2024-11-14 02:55:45

这与 FirstOrDefault 无关,但这是一个常见问题,可以通过静态 Object.Equals 方法解决。你想要:

MyObject obj = objList.FirstOrDefault(o => Object.Equals(o.Parent, x.Parent));

顺便说一句,该方法看起来像这样:

public static bool Equals(Object objA, Object objB) 
{
    // handle situation where both objects are null or both the same reference
    if (objA == objB)
        return true;
    // both are not null, so if any is null they can't be equal
    if (objA == null || objB == null)
        return false; 
    // nulls are already handled, so it's now safe to call objA.Equals
    return objA.Equals(objB);
} 

即使该方法不存在,你仍然可以这样编写你的作业:

MyObject obj = objList.FirstOrDefault(x.Parent == null ?
    o => o.Parent == null :
    o => x.Parent.Equals(o.Parent));

根据 x.Parent 是否为 null 使用不同的 lambda 。如果为 null,则只需查找 Parent 为 null 的对象。如果没有,调用 x.Parent.Equals 并使用 lambda 始终是安全的。

This has nothing to do with FirstOrDefault, but it is a common problem that is solved by the static Object.Equals method. You want:

MyObject obj = objList.FirstOrDefault(o => Object.Equals(o.Parent, x.Parent));

Incidentally, that method looks something like this:

public static bool Equals(Object objA, Object objB) 
{
    // handle situation where both objects are null or both the same reference
    if (objA == objB)
        return true;
    // both are not null, so if any is null they can't be equal
    if (objA == null || objB == null)
        return false; 
    // nulls are already handled, so it's now safe to call objA.Equals
    return objA.Equals(objB);
} 

Even if that method didn't exist, you could still write your assignment this way:

MyObject obj = objList.FirstOrDefault(x.Parent == null ?
    o => o.Parent == null :
    o => x.Parent.Equals(o.Parent));

That uses a different lambda depending on whether x.Parent is null. If it's null, it just has to look for objects whose Parent is null. If not, it's always safe to call x.Parent.Equals and uses a lambda that does so.

茶色山野 2024-11-14 02:55:45

您可以使用object.Equals代替。

MyObject obj = objList.FirstOrDefault(o => object.Equals(n.Parent, x.Parent));

只要两个参数都不为空,object.Equals 将使用第一个参数 Equal 覆盖。

You can use object.Equals instead.

MyObject obj = objList.FirstOrDefault(o => object.Equals(n.Parent, x.Parent));

object.Equals will use the first parameters Equal override so long as both parameters are not null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文