c# - 林克?重构foreach

标签 c# linq

有没有更好的写法?在最近做了很多 JavaScript 之后,我觉得我对 C# 感到生疏了。这可以改进吗?

    foreach (var item in this.CartItems)
    {
        if (item.EffectivePrice != null)
        {
            this.CartItems[this.CartItems.IndexOf(item)].EffectivePrice = 
                CurrencyHelper.GetLocalizedCurrency(item.EffectivePrice);
        }
    }

最佳答案

好吧,您可以使用 fromwhere 用 LINQ 查询语法编写它,但我不确定它是变化;我更想知道查找是否不必要:

this.CartItems[this.CartItems.IndexOf(item)].EffectivePrice = 
            CurrencyHelper.GetLocalizedCurrency(item.EffectivePrice);

到:

item.EffectivePrice = CurrencyHelper.GetLocalizedCurrency(item.EffectivePrice);

除此之外,我不确定是否值得进行更改;我可能会将其保留为:

foreach (var item in this.CartItems) {
    if (item.EffectivePrice != null) {
        item.EffectivePrice = CurrencyHelper.GetLocalizedCurrency(item.EffectivePrice);
    }
}

关于c# - 林克?重构foreach,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2115131/

相关文章:

c# - 使用.net框架和azure创建实时Web应用程序 - 非常困惑

c# - 计算内部列表中存在的元素数

.net - 如何将 linq 查询转换为人类可读的字符串

c# - 不带组的对象的 Linq 总和

c# - 如何在linq中制作if语句

xml - 将 XML 注释添加到由 LINQ to SQL 设计器生成的类属性

c# - n 秒后注视触发事件 - Unity

c# - 我可以加快此查询以检索域中的所有计算机吗?

c# - 如何获取当前端点合约的类型?

c# - .NET 2.0 还是 3.5?