2

我正在构建一个 JSON 字符串,在行部分中,我有两个值要构建并传入由辅助函数计算的 JSON 字符串。我想知道是否有办法调用这个辅助函数一次,然后返回一个值数组(在我的例子中是两个),所以我不必调用辅助函数两次(并且避免两次访问数据库)。

示例代码

            rows = (
                from tempItem in pagedQuery.ToList()
                select new
                {
                    cell = new string[] {                    
                        tempItem.Name,
                        tempItem.Regular,
                        HelperFunction.GetPrice(tempItem.ID, false).ToString(),
                        tempItem.Premium,
                        HelperFunction.GetPrice(tempItem.ID, true).ToString(),
                    }
                }).ToArray()

示例功能:

public decimal GetPrice(int ID, bool Premium)
{
  Item item = databaseCallToGetPrice(ID).first();

  if (Premium)
      return item.ExamplePrice;
  else
      return item.PremiumExamplePrice;
}

所以我要问的是在我的例子中我调用了 Helper 函数两次,有没有办法只调用一次然后返回一个数组,我可以以某种方式坚持然后使用两次。

4

2 回答 2

2

您可以使用LetAFAIK

rows = (
                from tempItem in pagedQuery.ToList()
                let prices = HelpererFunction.GetPrice(tempItem.ID)
                select new
                {
                    cell = new string[] {                    
                        tempItem.Name,
                        tempItem.Regular,
                        prices[0].ToString() ,
                        tempItem.Premium,
                        prices[1].ToString() ,
                    }
                }).ToArray()

您可以重构 GetPrice 函数以返回小数 []

public decimal[] GetPrice(int ID)
{
  Item item = databaseCallToGetPrice(ID).first();

  return new [] {item.ExamplePrice, item.PremiumExamplePrice};
}
于 2012-10-17T15:49:19.080 回答
2

Tuple<decimal, decimal>从函数返回 a :

public Tuple<decimal, decimal> GetPrices(int ID)
{
    Item item = databaseCallToGetPrice(ID).First();
    return Tuple.Create(item.ExamplePrice, item.PremiumExamplePrice);
}

然后使用它:

rows = (
        from tempItem in pagedQuery.ToList()
        let prices = HelpererFunction.GetPrices(tempItem.ID)
        select new
        {
            cell = new string[] {                    
                tempItem.Name,
                tempItem.Regular,
                prices.Item1.ToString(),
                tempItem.Premium,
                prices.Item2.ToString(),
            }
        }).ToArray()

恕我直言,元组比简单的数组更好,因为您可以保证从函数中返回两个且只有两个项目。


如果你想更清楚,你可以创建一个结构

public struct Prices
{
    public decimal PremiumPrice, Price;       

    public Prices(decimal premium, decimal price)
    {
        PremiumPrice = premium;
        Price = price;
    } 
}

然后从辅助函数返回

public Prices GetPrices(int ID)
{
    Item item = databaseCallToGetPrice(ID).First();
    return new Prices(item.PremiumExamplePrice, item.ExamplePrice);
}

并使用它:

rows = (
        from tempItem in pagedQuery.ToList()
        let prices = HelpererFunction.GetPrices(tempItem.ID)
        select new
        {
            cell = new string[] {                    
                tempItem.Name,
                tempItem.Regular,
                prices.Price.ToString(),
                tempItem.Premium,
                prices.PremiumPrice.ToString(),
            }
        }).ToArray()
于 2012-10-17T15:51:10.760 回答