繁体   English   中英

ASP.NET MVC / C#:我可以避免在单行C#条件语句中重复自己吗?

[英]ASP.NET MVC/C#: Can I avoid repeating myself in a one-line C# conditional statement?

在视图中的表中显示Customer的邮件地址时,请考虑以下代码:

<%: Customer.MailingAddress == null ? "" : Customer.MailingAddress.City %>

我发现自己使用了相当数量的这些三元条件语句,我想知道是否有一种方法可以在条件中引用回被评估的对象,以便我可以在表达式中使用它。 也许是这样的事情:

<%: Customer.MailingAddress == null ? "" : {0}.City %>

这样的事情存在吗? 我知道我可以创建一个变量来保存值,但是将所有内容保存在视图页面中的一个紧凑的小语句中会很好。

谢谢!

你可以用?? 运算符用于与null进行比较。

Customer.MailingAddress == null ? "" : Customer.MailingAddress;

以上可以改写如下:

Customer.MailingAddress ?? "";

在你的情况下,我通常创建扩展方法:

public static TValue GetSafe<T, TValue>(this T obj, Func<T, TValue> propertyExtractor)
where T : class
{
    if (obj != null)
    {
        return propertyExtractor(obj);
    }

    return null;
}

使用方式如下:

Customer.GetSafe(c => c.MailingAddress).GetSafe(ma => ma.City) ?? string.Empty

不,没有办法在不创建变量或复制自己的情况下准确地执行您的要求,尽管您可以执行以下操作:

(Customer.MailingAddress ?? new MailingAddress()).City ?? string.Empty

这假定默认情况下新的MailingAddress将使其city属性/字段为null。

如果创建新的MailingAddress将city字段/属性初始化为空字符串,则可以删除最后一个空合并。

但这实际上并不短,而且在我看来更为苛刻,而且几乎肯定不那么高效。

为什么不在包含该条件的Customer对象上创建属性并直接调用它?

Customer.FormattedMailingAddress

我会编写代码但是我在移动设备上,你只需要在get{}相同的条件。

我同意@Dave,为您的Customer类创建一个扩展。

像这样的东西:

public static class CustomerExtension
{
    public static string DisplayCity(this Customer customer)
    {
        return customer.MailingAddress == null ? String.Empty : customer.MailingAddress.City
    }
}

然后你可以像这样调用你的方法:

myCustomer.DisplayCity();

(注意:扩展不能作为属性创建,因此这必须是一个方法。有关详细信息,请参阅C#是否具有扩展属性?

您可以创建一个扩展方法来获取值或返回一个空字符串:

    public static string GetValue<T>(this T source, Func<T, string> getter)
    {
        if (source != null)
            return getter(source);

        return "";
    }

然后叫它:

<%: Customer.MailingAddress.GetValue(x=>x.City) %>

这适用于任何对象。

暂无
暂无

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

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