【发布时间】:2023-04-03 03:55:01
【问题描述】:
我编写了一个类,其中包含对从我的数据库中提取的数据执行计算的不同方法。每个方法都从数据库中提取信息并对其执行计算,然后将其保存到类中的一个变量中,当我初始化类时可以访问该变量。我在下面有两种伪代码方法,我正在尝试找出以下内容:
- 如果每个方法都提取数据然后对其执行计算,我应该以哪种方式使用 using 语句?
- 我是否使用最有效的方式来存储数据?
这是我在初始化类时所做的
public class Calculations
{
public string Symbol { get; set; }
public string Market { get; set; }
public List<decimal> stockPctReturns { get; set; }
public List<decimal> marketPctReturns { get; set; }
public enum returnType { Market, Stock };
public decimal avgAnnualMarketGrowth { get; set; }
public List<decimal> stockPctGains { get; set; }
public List<decimal> stockPctLosses { get; set; }
public List<decimal> positiveMoneyFlow { get; set; }
public List<decimal> negativeMoneyFlow { get; set; }
public decimal rsi { get; set; }
public decimal mfi { get; set; }
public decimal williamsR { get; set; }
public decimal sma20 { get; set; }
public decimal sma50 { get; set; }
public decimal sma200 { get; set; }
public const decimal riskFree10Yr = 2.58M;
public Calculations(string symbol, string market)
{
// initialize everything in the class
Symbol = symbol;
Market = market;
}
}
这样做:
public void fillPctReturns()
{
stockPctReturns = new List<decimal>();
marketPctReturns = new List<decimal>();
using (DailyGlobalDataTableAdapter dailyGlobalAdapter = new DailyGlobalDataTableAdapter())
using (DailyAmexDataTableAdapter dailyAmexAdapter = new DailyAmexDataTableAdapter())
using (DailyGlobalDataTable dailyGlobalTable = new DailyGlobalDataTable())
using (DailyAmexDataDataTable dailyAmexTable = new DailyAmexDataDataTable())
{
dailyAmexAdapter.Fill(dailyAmexTable);
dailyGlobalAdapter.Fill(dailyGlobalTable);
var amexQuery = from c in dailyGlobalTable.AsEnumerable()
where c.Date >= DateTime.Now.Subtract(TimeSpan.FromDays(60))
orderby c.Date descending
join d in dailyAmexTable.AsEnumerable() on c.Date equals d.Date
select new StockMarketCompare { stockClose = d.AdjustedClose, marketClose = c.AdjustedClose };
List<StockMarketCompare> amexResult = amexQuery.ToList();
// perform calculations here and save to stockPctReturns and marketPctReturns
}
}
或者我应该这样做:
using (DailyGlobalDataTableAdapter dailyGlobalAdapter = new DailyGlobalDataTableAdapter())
using (DailyAmexDataTableAdapter dailyAmexAdapter = new DailyAmexDataTableAdapter())
{
Calculations calc = new Calculations();
calc.Symbol = "GOOG";
calc.Market = "nyse";
dailyAmexAdapter.Fill(calc.dailyAmexTable);
dailyGlobalAdapter.Fill(calc.dailyGlobalTable);
calc.fillPctReturns();
}
// 这个例子在数据表的计算类中有公共属性,你可以看到我像上面那样设置它们
希望你能说出我想要做什么。
【问题讨论】:
标签: c# sql strongly-typed-dataset